fix library list
[platform/upstream/flac.git] / src / test_libFLAC++ / file_utils.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 "file_utils.h"
20 #include "FLAC/assert.h"
21 #include "FLAC/stream_encoder.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h> /* for stat() */
25
26 #ifdef min
27 #undef min
28 #endif
29 #define min(a,b) ((a)<(b)?(a):(b))
30
31 typedef struct {
32         FILE *file;
33 } encoder_client_struct;
34
35 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
36 {
37         encoder_client_struct *ecd = (encoder_client_struct*)client_data;
38
39         (void)encoder, (void)samples, (void)current_frame;
40
41         if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
42                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
43         else
44                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
45 }
46
47 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
48 {
49         (void)encoder, (void)metadata, (void)client_data;
50 }
51
52 FLAC__bool file_utils__generate_flacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
53 {
54         FLAC__int32 samples[1024];
55         FLAC__StreamEncoder *encoder;
56         encoder_client_struct encoder_client_data;
57         unsigned i, n;
58
59         FLAC__ASSERT(0 != output_filename);
60         FLAC__ASSERT(0 != streaminfo);
61         FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
62         FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
63
64         if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
65                 return false;
66
67         encoder = FLAC__stream_encoder_new();
68         if(0 == encoder) {
69                 fclose(encoder_client_data.file);
70                 return false;
71         }
72
73         FLAC__stream_encoder_set_verify(encoder, true);
74         FLAC__stream_encoder_set_streamable_subset(encoder, true);
75         FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
76         FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
77         FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
78         FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
79         FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
80         FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
81         FLAC__stream_encoder_set_max_lpc_order(encoder, 0);
82         FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
83         FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
84         FLAC__stream_encoder_set_do_escape_coding(encoder, false);
85         FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
86         FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
87         FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
88         FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
89         FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
90         FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
91         FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
92         FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
93         FLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
94
95         if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK) {
96                 fclose(encoder_client_data.file);
97                 return false;
98         }
99
100         /* init the dummy sample buffer */
101         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
102                 samples[i] = i & 7;
103
104         while(length > 0) {
105                 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
106
107                 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
108                         fclose(encoder_client_data.file);
109                         return false;
110                 }
111
112                 length -= n;
113         }
114
115         FLAC__stream_encoder_finish(encoder);
116
117         fclose(encoder_client_data.file);
118
119         FLAC__stream_encoder_delete(encoder);
120
121         if(0 != output_filesize) {
122                 struct stat filestats;
123
124                 if(stat(output_filename, &filestats) != 0)
125                         return false;
126                 else
127                         *output_filesize = (unsigned)filestats.st_size;
128         }
129
130         return true;
131 }