1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "FLAC/assert.h"
25 #include "FLAC/stream_encoder.h"
26 #include "test_libs_common/file_utils_flac.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/stat.h> /* for stat() */
30 #include "share/compat.h"
31
32 #ifdef min
33 #undef min
34 #endif
35 #define min(a,b) ((a)<(b)?(a):(b))
36
37 const long file_utils__ogg_serial_number = 12345;
38
39 #ifdef FLAC__VALGRIND_TESTING
local__fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream)40 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
41 {
42 size_t ret = fwrite(ptr, size, nmemb, stream);
43 if(!ferror(stream))
44 fflush(stream);
45 return ret;
46 }
47 #else
48 #define local__fwrite fwrite
49 #endif
50
51 typedef struct {
52 FILE *file;
53 } encoder_client_struct;
54
encoder_write_callback_(const FLAC__StreamEncoder * encoder,const FLAC__byte buffer[],size_t bytes,uint32_t samples,uint32_t current_frame,void * client_data)55 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data)
56 {
57 encoder_client_struct *ecd = (encoder_client_struct*)client_data;
58
59 (void)encoder, (void)samples, (void)current_frame;
60
61 if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
62 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
63 else
64 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
65 }
66
encoder_metadata_callback_(const FLAC__StreamEncoder * encoder,const FLAC__StreamMetadata * metadata,void * client_data)67 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
68 {
69 (void)encoder, (void)metadata, (void)client_data;
70 }
71
file_utils__generate_flacfile(FLAC__bool is_ogg,const char * output_filename,FLAC__off_t * output_filesize,uint32_t length,const FLAC__StreamMetadata * streaminfo,FLAC__StreamMetadata ** metadata,uint32_t num_metadata)72 FLAC__bool file_utils__generate_flacfile(FLAC__bool is_ogg, const char *output_filename, FLAC__off_t *output_filesize, uint32_t length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, uint32_t num_metadata)
73 {
74 FLAC__int32 samples[1024];
75 FLAC__StreamEncoder *encoder;
76 FLAC__StreamEncoderInitStatus init_status;
77 encoder_client_struct encoder_client_data;
78 uint32_t i, n;
79
80 FLAC__ASSERT(0 != output_filename);
81 FLAC__ASSERT(0 != streaminfo);
82 FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
83 FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
84
85 if(0 == (encoder_client_data.file = flac_fopen(output_filename, "wb")))
86 return false;
87
88 encoder = FLAC__stream_encoder_new();
89 if(0 == encoder) {
90 fclose(encoder_client_data.file);
91 return false;
92 }
93
94 FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number);
95 FLAC__stream_encoder_set_verify(encoder, true);
96 FLAC__stream_encoder_set_streamable_subset(encoder, true);
97 FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
98 FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
99 FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
100 FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
101 FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
102 FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
103 FLAC__stream_encoder_set_max_lpc_order(encoder, 0);
104 FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
105 FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
106 FLAC__stream_encoder_set_do_escape_coding(encoder, false);
107 FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
108 FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
109 FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
110 FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
111 FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
112 FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
113
114 if(is_ogg)
115 init_status = FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data);
116 else
117 init_status = FLAC__stream_encoder_init_stream(encoder, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data);
118
119 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
120 fclose(encoder_client_data.file);
121 return false;
122 }
123
124 /* init the dummy sample buffer */
125 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
126 samples[i] = i & 7;
127
128 while(length > 0) {
129 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
130
131 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
132 fclose(encoder_client_data.file);
133 return false;
134 }
135
136 length -= n;
137 }
138
139 (void)FLAC__stream_encoder_finish(encoder);
140
141 fclose(encoder_client_data.file);
142
143 FLAC__stream_encoder_delete(encoder);
144
145 if(0 != output_filesize) {
146 struct flac_stat_s filestats;
147
148 if(flac_stat(output_filename, &filestats) != 0)
149 return false;
150 else
151 *output_filesize = filestats.st_size;
152 }
153
154 return true;
155 }
156