-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflac.c
More file actions
279 lines (222 loc) · 12 KB
/
flac.c
File metadata and controls
279 lines (222 loc) · 12 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Created by claunia on 20/10/21.
//
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "3rdparty/flac/include/FLAC/metadata.h"
#include "3rdparty/flac/include/FLAC/stream_decoder.h"
#include "3rdparty/flac/include/FLAC/stream_encoder.h"
#include "library.h"
#include "flac.h"
static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t * bytes,
void * client_data);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder,
const FLAC__Frame * frame,
const FLAC__int32 *const buffer[],
void * client_data);
static void error_callback(const FLAC__StreamDecoder * decoder,
FLAC__StreamDecoderErrorStatus status,
void * client_data);
AARU_EXPORT size_t AARU_CALL AARU_flac_decode_redbook_buffer(uint8_t * dst_buffer,
size_t dst_size,
const uint8_t *src_buffer,
size_t src_size)
{
FLAC__StreamDecoder * decoder;
FLAC__StreamDecoderInitStatus init_status;
aaru_flac_ctx * ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
size_t ret_size;
memset(ctx, 0, sizeof(aaru_flac_ctx));
ctx->src_buffer = src_buffer;
ctx->src_len = src_size;
ctx->src_pos = 0;
ctx->dst_buffer = dst_buffer;
ctx->dst_len = dst_size;
ctx->dst_pos = 0;
ctx->error = 0;
decoder = FLAC__stream_decoder_new();
if(!decoder)
{
free(ctx);
return -1;
}
FLAC__stream_decoder_set_md5_checking(decoder, false);
init_status = FLAC__stream_decoder_init_stream(decoder, read_callback, // 1
NULL, // 2 seek
NULL, // 3 tell
NULL, // 4 length
NULL, // 5 eof
write_callback, // 6 write
NULL, // 7 metadata
error_callback, // 8 error
ctx // 9 client_data
);
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
{
free(ctx);
return -1;
}
// TODO: Return error somehow
FLAC__stream_decoder_process_until_end_of_stream(decoder);
FLAC__stream_decoder_delete(decoder);
ret_size = ctx->dst_pos;
free(ctx);
return ret_size;
}
static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t * bytes,
void * client_data)
{
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
if(ctx->src_len - ctx->src_pos < *bytes) *bytes = ctx->src_len - ctx->src_pos;
if(*bytes == 0) return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
memcpy(buffer, ctx->src_buffer + ctx->src_pos, *bytes);
ctx->src_pos += *bytes;
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder,
const FLAC__Frame * frame,
const FLAC__int32 *const buffer[],
void * client_data)
{
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
size_t i;
uint16_t * buffer16 = (uint16_t *)(ctx->dst_buffer + ctx->dst_pos);
// Why FLAC does not interleave the channels as PCM do, oh the mistery, we could use memcpy instead of looping
for(i = 0; i < frame->header.blocksize && ctx->dst_pos < ctx->dst_len; i++)
{
// Left channel
*(buffer16++) = (FLAC__int16)buffer[0][i];
// Right channel
*(buffer16++) = (FLAC__int16)buffer[1][i];
ctx->dst_pos += 4;
/* TODO: Big-endian (use bswap?)
// Left channel
ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[0][i];
ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[0][i] >> 8;
// Right channel
ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[1][i];
ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[1][i] >> 8;
*/
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
ctx->error = 1;
}
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);
AARU_EXPORT size_t AARU_CALL AARU_flac_encode_redbook_buffer(uint8_t * dst_buffer,
size_t dst_size,
const uint8_t *src_buffer,
size_t src_size,
uint32_t blocksize,
int32_t do_mid_side_stereo,
int32_t loose_mid_side_stereo,
const char * apodization,
uint32_t max_lpc_order,
uint32_t qlp_coeff_precision,
int32_t do_qlp_coeff_prec_search,
int32_t do_exhaustive_model_search,
uint32_t min_residual_partition_order,
uint32_t max_residual_partition_order,
const char * application_id,
uint32_t application_id_len)
{
FLAC__StreamEncoder * encoder;
aaru_flac_ctx * ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
FLAC__StreamEncoderInitStatus init_status;
size_t ret_size;
FLAC__int32 * pcm;
int i;
int16_t * buffer16 = (int16_t *)src_buffer;
FLAC__StreamMetadata * metadata[1];
memset(ctx, 0, sizeof(aaru_flac_ctx));
ctx->src_buffer = src_buffer;
ctx->src_len = src_size;
ctx->src_pos = 0;
ctx->dst_buffer = dst_buffer;
ctx->dst_len = dst_size;
ctx->dst_pos = 0;
ctx->error = 0;
encoder = FLAC__stream_encoder_new();
if(!encoder)
{
free(ctx);
return -1;
}
// TODO: Error detection here
FLAC__stream_encoder_set_verify(encoder, false);
FLAC__stream_encoder_set_streamable_subset(encoder, false);
FLAC__stream_encoder_set_channels(encoder, 2);
FLAC__stream_encoder_set_bits_per_sample(encoder, 16);
FLAC__stream_encoder_set_sample_rate(encoder, 44100);
FLAC__stream_encoder_set_blocksize(encoder, blocksize);
// true compresses more
FLAC__stream_encoder_set_do_mid_side_stereo(encoder, do_mid_side_stereo);
// false compresses more
FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, loose_mid_side_stereo);
// Apodization
FLAC__stream_encoder_set_apodization(encoder, apodization);
FLAC__stream_encoder_set_max_lpc_order(encoder, max_lpc_order);
FLAC__stream_encoder_set_qlp_coeff_precision(encoder, qlp_coeff_precision);
FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, do_qlp_coeff_prec_search);
FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, do_exhaustive_model_search);
FLAC__stream_encoder_set_min_residual_partition_order(encoder, min_residual_partition_order);
FLAC__stream_encoder_set_max_residual_partition_order(encoder, max_residual_partition_order);
FLAC__stream_encoder_set_total_samples_estimate(encoder, src_size / 4);
/* TODO: This is ignored by FLAC, need to replace it
if((metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) != NULL)
{
memset(&vorbis_entry, 0, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
vorbis_entry.entry = (unsigned char *)"Aaru.Compression.Native";
vorbis_entry.length = strlen("Aaru.Compression.Native");
FLAC__metadata_object_vorbiscomment_set_vendor_string(metadata[0], vorbis_entry, true);
}
*/
if(application_id_len > 0 && application_id != NULL)
if((metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)) != NULL)
FLAC__metadata_object_application_set_data(metadata[0], (unsigned char *)application_id, application_id_len,
true);
FLAC__stream_encoder_set_metadata(encoder, metadata, 1);
init_status = FLAC__stream_encoder_init_stream(encoder, encoder_write_callback, NULL, NULL, NULL, ctx);
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
{
free(ctx);
return -1;
}
pcm = malloc((src_size / 2) * sizeof(FLAC__int32));
for(i = 0; i < src_size / 2; i++) pcm[i] = (FLAC__int32)*(buffer16++);
FLAC__stream_encoder_process_interleaved(encoder, pcm, src_size / 4);
FLAC__stream_encoder_finish(encoder);
FLAC__stream_encoder_delete(encoder);
ret_size = ctx->dst_pos;
free(ctx);
free(pcm);
FLAC__metadata_object_delete(metadata[0]);
return ret_size;
}
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)
{
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
if(bytes > ctx->dst_len - ctx->dst_pos) bytes = ctx->dst_len - ctx->dst_pos;
memcpy(ctx->dst_buffer + ctx->dst_pos, buffer, bytes);
ctx->dst_pos += bytes;
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}