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