move in some common metadata setting stuff here
[flac.git] / src / test_libOggFLAC / file_utils.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 "file_utils.h"
20 #include "FLAC/assert.h"
21 #include "OggFLAC/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 const long file_utils__serial_number = 12345;
32
33 #ifdef FLAC__VALGRIND_TESTING
34 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
35 {
36         size_t ret = fwrite(ptr, size, nmemb, stream);
37         if(!ferror(stream))
38                 fflush(stream);
39         return ret;
40 }
41 #else
42 #define local__fwrite fwrite
43 #endif
44
45 typedef struct {
46         FILE *file;
47 } encoder_client_struct;
48
49 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
50 {
51         encoder_client_struct *ecd = (encoder_client_struct*)client_data;
52
53         (void)encoder, (void)samples, (void)current_frame;
54
55         if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
56                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
57         else
58                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
59 }
60
61 FLAC__bool file_utils__generate_oggflacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
62 {
63         FLAC__int32 samples[1024];
64         OggFLAC__StreamEncoder *encoder;
65         encoder_client_struct encoder_client_data;
66         unsigned i, n;
67
68         FLAC__ASSERT(0 != output_filename);
69         FLAC__ASSERT(0 != streaminfo);
70         FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
71         FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
72
73         if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
74                 return false;
75
76         encoder = OggFLAC__stream_encoder_new();
77         if(0 == encoder) {
78                 fclose(encoder_client_data.file);
79                 return false;
80         }
81
82         OggFLAC__stream_encoder_set_serial_number(encoder, file_utils__serial_number);
83         OggFLAC__stream_encoder_set_verify(encoder, true);
84         OggFLAC__stream_encoder_set_streamable_subset(encoder, true);
85         OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
86         OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
87         OggFLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
88         OggFLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
89         OggFLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
90         OggFLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
91         OggFLAC__stream_encoder_set_max_lpc_order(encoder, 0);
92         OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
93         OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
94         OggFLAC__stream_encoder_set_do_escape_coding(encoder, false);
95         OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
96         OggFLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
97         OggFLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
98         OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
99         OggFLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
100         OggFLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
101         OggFLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
102         OggFLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
103
104         if(OggFLAC__stream_encoder_init(encoder) != OggFLAC__STREAM_ENCODER_OK) {
105                 fclose(encoder_client_data.file);
106                 return false;
107         }
108
109         /* init the dummy sample buffer */
110         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
111                 samples[i] = i & 7;
112
113         while(length > 0) {
114                 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
115
116                 if(!OggFLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
117                         fclose(encoder_client_data.file);
118                         return false;
119                 }
120
121                 length -= n;
122         }
123
124         OggFLAC__stream_encoder_finish(encoder);
125
126         fclose(encoder_client_data.file);
127
128         OggFLAC__stream_encoder_delete(encoder);
129
130         if(0 != output_filesize) {
131                 struct stat filestats;
132
133                 if(stat(output_filename, &filestats) != 0)
134                         return false;
135                 else
136                         *output_filesize = (unsigned)filestats.st_size;
137         }
138
139         return true;
140 }