~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/flac.c -rw-r--r-- 5.6 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
#include <string.h>

#include <FLAC/stream_decoder.h>

#include "filter.h"
#include "flac.h"
#include "misc.h"
#include "tag.h"
#include "log.h"


static void metadata_callback(const FLAC__StreamDecoder *decoder,
	const FLAC__StreamMetadata *metadata, void *client_data)
{
	(void)decoder;
	Decoder *dec = client_data;

	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
	{
		dec->tinfo->format.bits = metadata->data.stream_info.bits_per_sample;
		dec->tinfo->format.channels = metadata->data.stream_info.channels;
		dec->tinfo->format.rate = metadata->data.stream_info.sample_rate;

		dec->tinfo->duration = duration_init(
			metadata->data.stream_info.total_samples,
			metadata->data.stream_info.sample_rate);
		dec->tinfo->position = duration_init(
			0,
			metadata->data.stream_info.sample_rate);
		dec->tinfo->bitrate = (double)file_size(dec->tinfo->path) *
			metadata->data.stream_info.bits_per_sample /
			((double)metadata->data.stream_info.total_samples /
				metadata->data.stream_info.sample_rate) /
			metadata->data.stream_info.channels;
	}
	else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
	{
		for (size_t i = 0; i < metadata->data.vorbis_comment.num_comments; ++i)
		{
			tag_vorbis_comment_parse(
				(const char *)metadata->data.vorbis_comment.comments[i].entry,
				dec->tinfo->tags);
		}
	}
}

static void error_callback(const FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	(void)decoder;
	Decoder *dec = client_data;

	if (!dec->cancel_flag)
	{
		log_append(LOG_ERR, "%s: FLAC decoding error (%s)", dec->tinfo->path,
			FLAC__StreamDecoderErrorStatusString[status]);
	}
}

#define FILTER_BUF_SIZE (BUFSIZ * 2)
static int16_t filter_buf[FILTER_BUF_SIZE];

static bool process_frame(const FLAC__int32 * const buffer[],
	const FLAC__Frame *frame, Decoder *dec)
{
	/* Check if main want us to die */
	if (dec->cancel_flag)
		return false;

	const Track_info *tinfo = dec->tinfo;
	const size_t nb_sample = frame->header.blocksize,
		nb_chan = frame->header.channels;
	tinfo->position->nb_samples += nb_sample;

	switch (frame->header.channels)
	{
		case 1:
		{
			const int32_t *restrict bufp = *buffer;
			for (size_t chunk, chunk_size, i = 0; i < nb_sample; i += chunk)
			{
				chunk = u64min(nb_sample - i, FILTER_BUF_SIZE);
				chunk_size = chunk * nb_chan;
				for (size_t j = 0; j < chunk; ++j) /*int32 to int16*/
					filter_buf[j] = *bufp++;
				apply_gain(filter_buf, chunk_size, tinfo->gain);
				if (!write_pcm(filter_buf, dec->pipefd_wr, chunk_size))
					return false;
			}
		}
		break;

		case 2:
		{
			const int32_t *restrict bufp[2] = {buffer[0], buffer[1]};
			for (size_t chunk, chunk_size, i = 0; i < nb_sample; i += chunk)
			{
				chunk = u64min(nb_sample - i, FILTER_BUF_SIZE / 2);
				chunk_size = chunk * nb_chan;
				interleave16_stereo(bufp, filter_buf, chunk);
				apply_gain(filter_buf, chunk_size, tinfo->gain);
				if (!write_pcm(filter_buf, dec->pipefd_wr, chunk_size))
					return false;
			}
		}
		break;

		default:
			log_append(LOG_ERR, "%s: only stereo and mono are supported",
				tinfo->path);
			return false;
	}
	return true;
}

static FLAC__StreamDecoderWriteStatus write_callback(
	const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
	const FLAC__int32 * const buffer[], void *client_data)
{
	(void)decoder;
	Decoder *dec = client_data;

	switch (frame->header.bits_per_sample)
	{
		case 16:
			if (!process_frame(buffer, frame, dec))
				return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
			break;

		default:
			log_append(LOG_ERR, "%s: only 16 bits FLAC is supported",
				dec->tinfo->path);
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
	}
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

void * flac_init(int fd, Decoder *dec)
{
	/* if (dec->aufmt == AUDIO_FORMAT_OGG_FLAC && FLAC_API_SUPPORTS_OGG_FLAC != 1) */
	/* { */
	/* 	log_append(LOG_ERR, "%s: libflac compiled without Ogg support"); */
	/* 	return NULL; */
	/* } */
	const char *path = dec->tinfo->path;
	if (dec->tinfo->aufmt == AUDIO_FORMAT_OGG_FLAC)
	{
		log_append(LOG_ERR, "%s: Ogg FLAC isn't yet supported", path);
		return NULL;
	}

	FLAC__StreamDecoder *ret = NULL;
	FLAC__StreamDecoderInitStatus init_status;

	if ((ret = FLAC__stream_decoder_new()) == NULL)
	{
		log_append(LOG_ERR, "%s: couldn't create FLAC decoder", path);
		return NULL;
	}
	FLAC__stream_decoder_set_md5_checking(ret, true);
	/* To parse Vorbis comments */
	FLAC__stream_decoder_set_metadata_respond_all(ret);
	FILE *stream = fdopen(fd, "rb");
	if (!stream)
	{
		log_append(LOG_ERR, "%s: couldn't fdopen", path);
		return NULL;
	}
	init_status = FLAC__stream_decoder_init_FILE(ret, stream,
		write_callback, metadata_callback, error_callback, dec);

	if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
	{
		log_append(LOG_ERR, "%s: FLAC decoder initialization error (%s)",
			path, FLAC__StreamDecoderInitStatusString[init_status]);
		FLAC__stream_decoder_delete(ret);
		return NULL;
	}
	if (!FLAC__stream_decoder_process_until_end_of_metadata(ret))
	{
		log_append(LOG_ERR, "%s: FLAC metadata decoding error (%s)", path,
			FLAC__stream_decoder_get_resolved_state_string(ret));
		return NULL;
	}

	return ret;
}

void flac_free(void *flac_dec)
{
	(void)FLAC__stream_decoder_finish(flac_dec);
	FLAC__stream_decoder_delete(flac_dec);
}

void * flac_routine(void *arg)
{
	const Decoder *dec = arg;
	FLAC__StreamDecoder *flac_dec = dec->ctx;

	if (!FLAC__stream_decoder_process_until_end_of_stream(flac_dec) &&
		!dec->cancel_flag)
	{
		log_append(LOG_ERR, "%s: FLAC stream decoder error (%s)",
			dec->tinfo->path,
			FLAC__stream_decoder_get_resolved_state_string(flac_dec));
	}
	decoder_end(dec);
	return NULL;
}