another big glob of changes/fixes
[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 #if defined _MSC_VER || defined __MINGW32__
25 #include <io.h> /* for chmod(), unlink */
26 #endif
27 #include <sys/stat.h> /* for stat(), chmod() */
28 #if defined _WIN32 && !defined __CYGWIN__
29 #else
30 #include <unistd.h> /* for unlink() */
31 #endif
32
33 #ifdef min
34 #undef min
35 #endif
36 #define min(a,b) ((a)<(b)?(a):(b))
37
38 typedef struct {
39         FILE *file;
40 } encoder_client_struct;
41
42 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
43 {
44         encoder_client_struct *ecd = (encoder_client_struct*)client_data;
45
46         (void)encoder, (void)samples, (void)current_frame;
47
48         if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
49                 return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
50         else
51                 return FLAC__STREAM_ENCODER_WRITE_OK;
52 }
53
54 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
55 {
56         (void)encoder, (void)metadata, (void)client_data;
57 }
58
59 FLAC__bool file_utils__change_stats(const char *filename, FLAC__bool read_only)
60 {
61         struct stat stats;
62
63         if(0 == stat(filename, &stats)) {
64 #if !defined _MSC_VER && !defined __MINGW32__
65                 if(read_only) {
66                         stats.st_mode &= ~S_IWUSR;
67                         stats.st_mode &= ~S_IWGRP;
68                         stats.st_mode &= ~S_IWOTH;
69                 }
70                 else {
71                         stats.st_mode |= S_IWUSR;
72                         stats.st_mode |= S_IWGRP;
73                         stats.st_mode |= S_IWOTH;
74                 }
75 #else
76                 if(read_only)
77                         stats.st_mode &= ~S_IWRITE;
78                 else
79                         stats.st_mode |= S_IWRITE;
80 #endif
81                 if(0 != chmod(filename, stats.st_mode))
82                         return false;
83         }
84         else
85                 return false;
86
87         return true;
88 }
89
90 FLAC__bool file_utils__remove_file(const char *filename)
91 {
92         return file_utils__change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
93 }
94
95 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)
96 {
97         FLAC__int32 samples[1024];
98         FLAC__StreamEncoder *encoder;
99         encoder_client_struct encoder_client_data;
100         unsigned i, n;
101
102         FLAC__ASSERT(0 != output_filename);
103         FLAC__ASSERT(0 != streaminfo);
104         FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
105         FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
106
107         if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
108                 return false;
109
110         encoder = FLAC__stream_encoder_new();
111         if(0 == encoder) {
112                 fclose(encoder_client_data.file);
113                 return false;
114         }
115
116         FLAC__stream_encoder_set_streamable_subset(encoder, true);
117         FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
118         FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
119         FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
120         FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
121         FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
122         FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
123         FLAC__stream_encoder_set_max_lpc_order(encoder, 0);
124         FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
125         FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
126         FLAC__stream_encoder_set_do_escape_coding(encoder, false);
127         FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
128         FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
129         FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
130         FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
131         FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
132         FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
133         FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
134         FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
135         FLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
136
137         if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK) {
138                 fclose(encoder_client_data.file);
139                 return false;
140         }
141
142         /* init the dummy sample buffer */
143         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
144                 samples[i] = i & 7;
145
146         while(length > 0) {
147                 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
148
149                 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
150                         fclose(encoder_client_data.file);
151                         return false;
152                 }
153
154                 length -= n;
155         }
156
157         FLAC__stream_encoder_finish(encoder);
158
159         fclose(encoder_client_data.file);
160
161         FLAC__stream_encoder_delete(encoder);
162
163         if(0 != output_filesize) {
164                 struct stat filestats;
165
166                 if(stat(output_filename, &filestats) != 0)
167                         return false;
168                 else
169                         *output_filesize = (unsigned)filestats.st_size;
170         }
171
172         return true;
173 }