libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
flac.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "aaruformat.h"
24
25#include "flac.h"
26#include "FLAC/metadata.h"
27#include "FLAC/stream_decoder.h"
28#include "FLAC/stream_encoder.h"
29
30static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
31 size_t *bytes, void *client_data);
32static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
33 const FLAC__int32 *const buffer[], void *client_data);
34static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
35 void *client_data);
36
48AARU_EXPORT size_t AARU_CALL aaruf_flac_decode_redbook_buffer(uint8_t *dst_buffer, size_t dst_size,
49 const uint8_t *src_buffer, size_t src_size)
50{
51 FLAC__StreamDecoder *decoder = NULL;
52 FLAC__StreamDecoderInitStatus init_status = FLAC__STREAM_DECODER_INIT_STATUS_OK;
53 aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
54 size_t ret_size = 0;
55
56 if(ctx == NULL) return 0;
57
58 memset(ctx, 0, sizeof(aaru_flac_ctx));
59
60 ctx->src_buffer = src_buffer;
61 ctx->src_len = src_size;
62 ctx->src_pos = 0;
63 ctx->dst_buffer = dst_buffer;
64 ctx->dst_len = dst_size;
65 ctx->dst_pos = 0;
66 ctx->error = 0;
67
68 decoder = FLAC__stream_decoder_new();
69
70 if(!decoder)
71 {
72 free(ctx);
73 return 0;
74 }
75
76 FLAC__stream_decoder_set_md5_checking(decoder, false);
77
78 init_status = FLAC__stream_decoder_init_stream(decoder, read_callback, NULL, NULL, NULL, NULL, write_callback, NULL,
79 error_callback, ctx);
80
81 if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
82 {
83 FLAC__stream_decoder_delete(decoder);
84 free(ctx);
85 return 0;
86 }
87
88 FLAC__bool ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
89
90 FLAC__stream_decoder_delete(decoder);
91
92 ret_size = ctx->dst_pos;
93 int had_error = ctx->error;
94
95 free(ctx);
96
97 if(!ok || had_error) return 0;
98
99 return ret_size;
100}
101
102static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
103 size_t *bytes, void *client_data)
104{
105 aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
106
107 if(ctx->src_len - ctx->src_pos < *bytes) *bytes = ctx->src_len - ctx->src_pos;
108
109 if(*bytes == 0) return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
110
111 memcpy(buffer, ctx->src_buffer + ctx->src_pos, *bytes);
112 ctx->src_pos += *bytes;
113
114 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
115}
116
117static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
118 const FLAC__int32 *const buffer[], void *client_data)
119{
120 aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
121 uint16_t *buffer16 = (uint16_t *)(ctx->dst_buffer + ctx->dst_pos);
122
123 // Why FLAC does not interleave the channels as PCM do, oh the mistery, we could use memcpy instead of looping
124 for(size_t i = 0; i < frame->header.blocksize && ctx->dst_pos < ctx->dst_len; i++)
125 {
126 // Left channel
127 *(buffer16++) = (FLAC__int16)buffer[0][i];
128 // Right channel
129 *(buffer16++) = (FLAC__int16)buffer[1][i];
130
131 ctx->dst_pos += 4;
132
133 /* TODO: Big-endian (use bswap?)
134 // Left channel
135 ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[0][i];
136 ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[0][i] >> 8;
137 // Right channel
138 ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[1][i];
139 ctx->dst_buffer[ctx->dst_pos++] = (FLAC__uint16)(FLAC__int16)buffer[1][i] >> 8;
140 */
141 }
142
143 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
144}
145
146static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
147{
148 aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
149
150 fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
151
152 ctx->error = 1;
153}
154
155static FLAC__StreamEncoderWriteStatus encoder_write_callback(const FLAC__StreamEncoder *encoder,
156 const FLAC__byte buffer[], size_t bytes, uint32_t samples,
157 uint32_t current_frame, void *client_data);
158
181 uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size, uint32_t blocksize,
182 int32_t do_mid_side_stereo, int32_t loose_mid_side_stereo, const char *apodization, uint32_t max_lpc_order,
183 uint32_t qlp_coeff_precision, int32_t do_qlp_coeff_prec_search, int32_t do_exhaustive_model_search,
184 uint32_t min_residual_partition_order, uint32_t max_residual_partition_order, const char *application_id,
185 uint32_t application_id_len)
186{
187 FLAC__StreamEncoder *encoder = NULL;
188 aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
189 FLAC__StreamEncoderInitStatus init_status = FLAC__STREAM_ENCODER_INIT_STATUS_OK;
190 size_t ret_size = 0;
191 FLAC__int32 *pcm = NULL;
192 int i = 0;
193 int16_t *buffer16 = (int16_t *)src_buffer;
194 FLAC__StreamMetadata *metadata[1] = {NULL};
195
196 if(ctx == NULL) return 0;
197
198 memset(ctx, 0, sizeof(aaru_flac_ctx));
199
200 ctx->src_buffer = src_buffer;
201 ctx->src_len = src_size;
202 ctx->src_pos = 0;
203 ctx->dst_buffer = dst_buffer;
204 ctx->dst_len = dst_size;
205 ctx->dst_pos = 0;
206 ctx->error = 0;
207
208 encoder = FLAC__stream_encoder_new();
209
210 if(!encoder)
211 {
212 free(ctx);
213 return 0;
214 }
215
216 FLAC__bool ok = true;
217 ok = ok && FLAC__stream_encoder_set_verify(encoder, false);
218 ok = ok && FLAC__stream_encoder_set_streamable_subset(encoder, false);
219 ok = ok && FLAC__stream_encoder_set_channels(encoder, 2);
220 ok = ok && FLAC__stream_encoder_set_bits_per_sample(encoder, 16);
221 ok = ok && FLAC__stream_encoder_set_sample_rate(encoder, 44100);
222 ok = ok && FLAC__stream_encoder_set_blocksize(encoder, blocksize);
223 ok = ok && FLAC__stream_encoder_set_do_mid_side_stereo(encoder, do_mid_side_stereo);
224 ok = ok && FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, loose_mid_side_stereo);
225 ok = ok && FLAC__stream_encoder_set_apodization(encoder, apodization);
226 ok = ok && FLAC__stream_encoder_set_max_lpc_order(encoder, max_lpc_order);
227 ok = ok && FLAC__stream_encoder_set_qlp_coeff_precision(encoder, qlp_coeff_precision);
228 ok = ok && FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, do_qlp_coeff_prec_search);
229 ok = ok && FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, do_exhaustive_model_search);
230 ok = ok && FLAC__stream_encoder_set_min_residual_partition_order(encoder, min_residual_partition_order);
231 ok = ok && FLAC__stream_encoder_set_max_residual_partition_order(encoder, max_residual_partition_order);
232 ok = ok && FLAC__stream_encoder_set_total_samples_estimate(encoder, src_size / 4);
233
234 if(!ok)
235 {
236 FLAC__stream_encoder_delete(encoder);
237 free(ctx);
238 return 0;
239 }
240
241 if(application_id_len > 0 && application_id != NULL)
242 if((metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)) != NULL)
243 FLAC__metadata_object_application_set_data(metadata[0], (unsigned char *)application_id, application_id_len,
244 true);
245
246 if(metadata[0] != NULL) FLAC__stream_encoder_set_metadata(encoder, metadata, 1);
247
248 init_status = FLAC__stream_encoder_init_stream(encoder, encoder_write_callback, NULL, NULL, NULL, ctx);
249
250 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
251 {
252 FLAC__stream_encoder_delete(encoder);
253 if(metadata[0] != NULL) FLAC__metadata_object_delete(metadata[0]);
254 free(ctx);
255 return 0;
256 }
257
258 pcm = malloc((src_size / 2) * sizeof(FLAC__int32));
259
260 if(pcm == NULL)
261 {
262 FLAC__stream_encoder_delete(encoder);
263 if(metadata[0] != NULL) FLAC__metadata_object_delete(metadata[0]);
264 free(ctx);
265 return 0;
266 }
267
268 for(i = 0; i < src_size / 2; i++) pcm[i] = (FLAC__int32) * (buffer16++);
269
270 ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, src_size / 4);
271
272 FLAC__stream_encoder_finish(encoder);
273
274 FLAC__stream_encoder_delete(encoder);
275
276 ret_size = ok ? ctx->dst_pos : 0;
277
278 free(ctx);
279 free(pcm);
280 if(metadata[0] != NULL) FLAC__metadata_object_delete(metadata[0]);
281
282 return ret_size;
283}
284
285static FLAC__StreamEncoderWriteStatus encoder_write_callback(const FLAC__StreamEncoder *encoder,
286 const FLAC__byte buffer[], size_t bytes, uint32_t samples,
287 uint32_t current_frame, void *client_data)
288{
289 aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
290
291 if(bytes > ctx->dst_len - ctx->dst_pos) bytes = ctx->dst_len - ctx->dst_pos;
292
293 memcpy(ctx->dst_buffer + ctx->dst_pos, buffer, bytes);
294
295 ctx->dst_pos += bytes;
296
297 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
298}
#define AARU_CALL
Definition decls.h:46
#define AARU_EXPORT
Definition decls.h:55
size_t aaruf_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)
Encodes a Red Book audio buffer to FLAC format.
Definition flac.c:180
static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Definition flac.c:102
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)
Definition flac.c:285
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
Definition flac.c:117
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
Definition flac.c:146
size_t aaruf_flac_decode_redbook_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size)
Decodes a FLAC-compressed Red Book audio buffer.
Definition flac.c:48
size_t dst_pos
Definition flac.h:29
uint8_t error
Definition flac.h:30
size_t dst_len
Definition flac.h:28
const uint8_t * src_buffer
Definition flac.h:24
size_t src_pos
Definition flac.h:26
size_t src_len
Definition flac.h:25
uint8_t * dst_buffer
Definition flac.h:27