Skip to content

Latest commit

 

History

History
155 lines (128 loc) · 5.49 KB

file_utils_flac.c

File metadata and controls

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