move common stuff into metadata_utils
[flac.git] / src / test_libOggFLAC / encoders.c
1 /* test_libOggFLAC - Unit tester for libOggFLAC
2  * Copyright (C) 2002,2003  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 "metadata_utils.h"
22 #include "FLAC/assert.h"
23 #include "OggFLAC/stream_encoder.h"
24 #include "share/grabbag.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_;
30 static FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_ };
31 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
32 static const char *oggflacfilename_ = "metadata.ogg";
33
34 static FLAC__bool die_s_(const char *msg, const OggFLAC__StreamEncoder *encoder)
35 {
36         OggFLAC__StreamEncoderState state = OggFLAC__stream_encoder_get_state(encoder);
37
38         if(msg)
39                 printf("FAILED, %s", msg);
40         else
41                 printf("FAILED");
42
43         printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__StreamEncoderStateString[state]);
44         if(state == OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
45                 FLAC__StreamEncoderState state_ = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(encoder);
46                 printf("      FLAC stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
47                 if(state_ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
48                         FLAC__StreamDecoderState dstate = OggFLAC__stream_encoder_get_verify_decoder_state(encoder);
49                         printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
50                 }
51         }
52
53         return false;
54 }
55
56 static void init_metadata_blocks_()
57 {
58         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_);
59 }
60
61 static void free_metadata_blocks_()
62 {
63         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_);
64 }
65
66 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)
67 {
68         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
69         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
70 }
71
72 static FLAC__bool test_stream_encoder()
73 {
74         OggFLAC__StreamEncoder *encoder;
75         OggFLAC__StreamEncoderState state;
76         FLAC__StreamEncoderState state_;
77         FLAC__StreamDecoderState dstate;
78         FLAC__int32 samples[1024];
79         FLAC__int32 *samples_array[1] = { samples };
80         unsigned i;
81
82         printf("\n+++ libOggFLAC unit test: OggFLAC__StreamEncoder\n\n");
83
84         printf("testing OggFLAC__stream_encoder_new()... ");
85         encoder = OggFLAC__stream_encoder_new();
86         if(0 == encoder) {
87                 printf("FAILED, returned NULL\n");
88                 return false;
89         }
90         printf("OK\n");
91
92         printf("testing OggFLAC__stream_encoder_set_serial_number()... ");
93         if(!OggFLAC__stream_encoder_set_serial_number(encoder, file_utils__serial_number))
94                 return die_s_("returned false", encoder);
95         printf("OK\n");
96
97         printf("testing OggFLAC__stream_encoder_set_verify()... ");
98         if(!OggFLAC__stream_encoder_set_verify(encoder, true))
99                 return die_s_("returned false", encoder);
100         printf("OK\n");
101
102         printf("testing OggFLAC__stream_encoder_set_streamable_subset()... ");
103         if(!OggFLAC__stream_encoder_set_streamable_subset(encoder, true))
104                 return die_s_("returned false", encoder);
105         printf("OK\n");
106
107         printf("testing OggFLAC__stream_encoder_set_do_mid_side_stereo()... ");
108         if(!OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
109                 return die_s_("returned false", encoder);
110         printf("OK\n");
111
112         printf("testing OggFLAC__stream_encoder_set_loose_mid_side_stereo()... ");
113         if(!OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
114                 return die_s_("returned false", encoder);
115         printf("OK\n");
116
117         printf("testing OggFLAC__stream_encoder_set_channels()... ");
118         if(!OggFLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
119                 return die_s_("returned false", encoder);
120         printf("OK\n");
121
122         printf("testing OggFLAC__stream_encoder_set_bits_per_sample()... ");
123         if(!OggFLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
124                 return die_s_("returned false", encoder);
125         printf("OK\n");
126
127         printf("testing OggFLAC__stream_encoder_set_sample_rate()... ");
128         if(!OggFLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
129                 return die_s_("returned false", encoder);
130         printf("OK\n");
131
132         printf("testing OggFLAC__stream_encoder_set_blocksize()... ");
133         if(!OggFLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
134                 return die_s_("returned false", encoder);
135         printf("OK\n");
136
137         printf("testing OggFLAC__stream_encoder_set_max_lpc_order()... ");
138         if(!OggFLAC__stream_encoder_set_max_lpc_order(encoder, 0))
139                 return die_s_("returned false", encoder);
140         printf("OK\n");
141
142         printf("testing OggFLAC__stream_encoder_set_qlp_coeff_precision()... ");
143         if(!OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
144                 return die_s_("returned false", encoder);
145         printf("OK\n");
146
147         printf("testing OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
148         if(!OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
149                 return die_s_("returned false", encoder);
150         printf("OK\n");
151
152         printf("testing OggFLAC__stream_encoder_set_do_escape_coding()... ");
153         if(!OggFLAC__stream_encoder_set_do_escape_coding(encoder, false))
154                 return die_s_("returned false", encoder);
155         printf("OK\n");
156
157         printf("testing OggFLAC__stream_encoder_set_do_exhaustive_model_search()... ");
158         if(!OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
159                 return die_s_("returned false", encoder);
160         printf("OK\n");
161
162         printf("testing OggFLAC__stream_encoder_set_min_residual_partition_order()... ");
163         if(!OggFLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
164                 return die_s_("returned false", encoder);
165         printf("OK\n");
166
167         printf("testing OggFLAC__stream_encoder_set_max_residual_partition_order()... ");
168         if(!OggFLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
169                 return die_s_("returned false", encoder);
170         printf("OK\n");
171
172         printf("testing OggFLAC__stream_encoder_set_rice_parameter_search_dist()... ");
173         if(!OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
174                 return die_s_("returned false", encoder);
175         printf("OK\n");
176
177         printf("testing OggFLAC__stream_encoder_set_total_samples_estimate()... ");
178         if(!OggFLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
179                 return die_s_("returned false", encoder);
180         printf("OK\n");
181
182         printf("testing OggFLAC__stream_encoder_set_metadata()... ");
183         if(!OggFLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
184                 return die_s_("returned false", encoder);
185         printf("OK\n");
186
187         printf("testing OggFLAC__stream_encoder_set_write_callback()... ");
188         if(!OggFLAC__stream_encoder_set_write_callback(encoder, stream_encoder_write_callback_))
189                 return die_s_("returned false", encoder);
190         printf("OK\n");
191
192         printf("testing OggFLAC__stream_encoder_set_client_data()... ");
193         if(!OggFLAC__stream_encoder_set_client_data(encoder, 0))
194                 return die_s_("returned false", encoder);
195         printf("OK\n");
196
197         printf("testing OggFLAC__stream_encoder_init()... ");
198         if(OggFLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
199                 return die_s_(0, encoder);
200         printf("OK\n");
201
202         printf("testing OggFLAC__stream_encoder_get_state()... ");
203         state = OggFLAC__stream_encoder_get_state(encoder);
204         printf("returned state = %u (%s)... OK\n", (unsigned)state, OggFLAC__StreamEncoderStateString[state]);
205
206         printf("testing OggFLAC__stream_encoder_get_FLAC_stream_encoder_state()... ");
207         state_ = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(encoder);
208         printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
209
210         printf("testing OggFLAC__stream_encoder_get_verify_decoder_state()... ");
211         dstate = OggFLAC__stream_encoder_get_verify_decoder_state(encoder);
212         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
213
214         {
215                 FLAC__uint64 absolute_sample;
216                 unsigned frame_number;
217                 unsigned channel;
218                 unsigned sample;
219                 FLAC__int32 expected;
220                 FLAC__int32 got;
221
222                 printf("testing OggFLAC__stream_encoder_get_verify_decoder_error_stats()... ");
223                 OggFLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
224                 printf("OK\n");
225         }
226
227         printf("testing OggFLAC__stream_encoder_get_verify()... ");
228         if(OggFLAC__stream_encoder_get_verify(encoder) != true) {
229                 printf("FAILED, expected true, got false\n");
230                 return false;
231         }
232         printf("OK\n");
233
234         printf("testing OggFLAC__stream_encoder_get_streamable_subset()... ");
235         if(OggFLAC__stream_encoder_get_streamable_subset(encoder) != true) {
236                 printf("FAILED, expected true, got false\n");
237                 return false;
238         }
239         printf("OK\n");
240
241         printf("testing OggFLAC__stream_encoder_get_do_mid_side_stereo()... ");
242         if(OggFLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
243                 printf("FAILED, expected false, got true\n");
244                 return false;
245         }
246         printf("OK\n");
247
248         printf("testing OggFLAC__stream_encoder_get_loose_mid_side_stereo()... ");
249         if(OggFLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
250                 printf("FAILED, expected false, got true\n");
251                 return false;
252         }
253         printf("OK\n");
254
255         printf("testing OggFLAC__stream_encoder_get_channels()... ");
256         if(OggFLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
257                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, OggFLAC__stream_encoder_get_channels(encoder));
258                 return false;
259         }
260         printf("OK\n");
261
262         printf("testing OggFLAC__stream_encoder_get_bits_per_sample()... ");
263         if(OggFLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
264                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, OggFLAC__stream_encoder_get_bits_per_sample(encoder));
265                 return false;
266         }
267         printf("OK\n");
268
269         printf("testing OggFLAC__stream_encoder_get_sample_rate()... ");
270         if(OggFLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
271                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, OggFLAC__stream_encoder_get_sample_rate(encoder));
272                 return false;
273         }
274         printf("OK\n");
275
276         printf("testing OggFLAC__stream_encoder_get_blocksize()... ");
277         if(OggFLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
278                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, OggFLAC__stream_encoder_get_blocksize(encoder));
279                 return false;
280         }
281         printf("OK\n");
282
283         printf("testing OggFLAC__stream_encoder_get_max_lpc_order()... ");
284         if(OggFLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
285                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_max_lpc_order(encoder));
286                 return false;
287         }
288         printf("OK\n");
289
290         printf("testing OggFLAC__stream_encoder_get_qlp_coeff_precision()... ");
291         (void)OggFLAC__stream_encoder_get_qlp_coeff_precision(encoder);
292         /* we asked the encoder to auto select this so we accept anything */
293         printf("OK\n");
294
295         printf("testing OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
296         if(OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
297                 printf("FAILED, expected false, got true\n");
298                 return false;
299         }
300         printf("OK\n");
301
302         printf("testing OggFLAC__stream_encoder_get_do_escape_coding()... ");
303         if(OggFLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
304                 printf("FAILED, expected false, got true\n");
305                 return false;
306         }
307         printf("OK\n");
308
309         printf("testing OggFLAC__stream_encoder_get_do_exhaustive_model_search()... ");
310         if(OggFLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
311                 printf("FAILED, expected false, got true\n");
312                 return false;
313         }
314         printf("OK\n");
315
316         printf("testing OggFLAC__stream_encoder_get_min_residual_partition_order()... ");
317         if(OggFLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
318                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_min_residual_partition_order(encoder));
319                 return false;
320         }
321         printf("OK\n");
322
323         printf("testing OggFLAC__stream_encoder_get_max_residual_partition_order()... ");
324         if(OggFLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
325                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_max_residual_partition_order(encoder));
326                 return false;
327         }
328         printf("OK\n");
329
330         printf("testing OggFLAC__stream_encoder_get_rice_parameter_search_dist()... ");
331         if(OggFLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
332                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
333                 return false;
334         }
335         printf("OK\n");
336
337         printf("testing OggFLAC__stream_encoder_get_total_samples_estimate()... ");
338         if(OggFLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
339                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, OggFLAC__stream_encoder_get_total_samples_estimate(encoder));
340                 return false;
341         }
342         printf("OK\n");
343
344         /* init the dummy sample buffer */
345         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
346                 samples[i] = i & 7;
347
348         printf("testing OggFLAC__stream_encoder_process()... ");
349         if(!OggFLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
350                 return die_s_("returned false", encoder);
351         printf("OK\n");
352
353         printf("testing OggFLAC__stream_encoder_process_interleaved()... ");
354         if(!OggFLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
355                 return die_s_("returned false", encoder);
356         printf("OK\n");
357
358         printf("testing OggFLAC__stream_encoder_finish()... ");
359         OggFLAC__stream_encoder_finish(encoder);
360         printf("OK\n");
361
362         printf("testing OggFLAC__stream_encoder_delete()... ");
363         OggFLAC__stream_encoder_delete(encoder);
364         printf("OK\n");
365
366         printf("\nPASSED!\n");
367
368         return true;
369 }
370
371 FLAC__bool test_encoders()
372 {
373         init_metadata_blocks_();
374
375         if(!test_stream_encoder())
376                 return false;
377
378         (void) grabbag__file_remove_file(oggflacfilename_);
379         free_metadata_blocks_();
380
381         return true;
382 }