forked from ralph-irving/squeezelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpg.c
277 lines (226 loc) · 6.96 KB
/
mpg.c
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
/*
* Squeezelite - lightweight headless squeezebox emulator
*
* (c) Adrian Smith 2012-2015, triode1@btinternet.com
* Ralph Irving 2015-2024, ralph_irving@hotmail.com
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "squeezelite.h"
#include <mpg123.h>
#define READ_SIZE 512
#define WRITE_SIZE 32 * 1024
struct mpg {
mpg123_handle *h;
bool use16bit;
#if !LINKALL
// mpg symbols to be dynamically loaded
int (* mpg123_init)(void);
int (* mpg123_feature)(const enum mpg123_feature_set);
void (* mpg123_rates)(const long **, size_t *);
int (* mpg123_format_none)(mpg123_handle *);
int (* mpg123_format)(mpg123_handle *, long, int, int);
mpg123_handle *(* mpg123_new)(const char*, int *);
void (* mpg123_delete)(mpg123_handle *);
int (* mpg123_open_feed)(mpg123_handle *);
int (* mpg123_decode)(mpg123_handle *, const unsigned char *, size_t, unsigned char *, size_t, size_t *);
int (* mpg123_getformat)(mpg123_handle *, long *, int *, int *);
const char* (* mpg123_plain_strerror)(int);
#endif
};
static struct mpg *m;
extern log_level loglevel;
extern struct buffer *streambuf;
extern struct buffer *outputbuf;
extern struct streamstate stream;
extern struct outputstate output;
extern struct decodestate decode;
extern struct processstate process;
#define LOCK_S mutex_lock(streambuf->mutex)
#define UNLOCK_S mutex_unlock(streambuf->mutex)
#define LOCK_O mutex_lock(outputbuf->mutex)
#define UNLOCK_O mutex_unlock(outputbuf->mutex)
#if PROCESS
#define LOCK_O_direct if (decode.direct) mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct if (decode.direct) mutex_unlock(outputbuf->mutex)
#define LOCK_O_not_direct if (!decode.direct) mutex_lock(outputbuf->mutex)
#define UNLOCK_O_not_direct if (!decode.direct) mutex_unlock(outputbuf->mutex)
#define IF_DIRECT(x) if (decode.direct) { x }
#define IF_PROCESS(x) if (!decode.direct) { x }
#else
#define LOCK_O_direct mutex_lock(outputbuf->mutex)
#define UNLOCK_O_direct mutex_unlock(outputbuf->mutex)
#define LOCK_O_not_direct
#define UNLOCK_O_not_direct
#define IF_DIRECT(x) { x }
#define IF_PROCESS(x)
#endif
#if LINKALL
#define MPG123(h, fn, ...) (mpg123_ ## fn)(__VA_ARGS__)
#else
#define MPG123(h, fn, ...) (h)->mpg123_##fn(__VA_ARGS__)
#endif
static decode_state mpg_decode(void) {
size_t bytes, space, size;
int ret;
u8_t *write_buf;
LOCK_S;
LOCK_O_direct;
bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
IF_DIRECT(
space = min(_buf_space(outputbuf), _buf_cont_write(outputbuf));
write_buf = outputbuf->writep;
);
IF_PROCESS(
space = process.max_in_frames;
write_buf = process.inbuf;
);
bytes = min(bytes, READ_SIZE);
space = min(space, WRITE_SIZE);
if (m->use16bit) {
space = (space / BYTES_PER_FRAME) * 4;
}
// only get the new stream information on first call so we can reset decode.direct appropriately
if (decode.new_stream) {
space = 0;
}
ret = MPG123(m, decode, m->h, streambuf->readp, bytes, write_buf, space, &size);
if (ret == MPG123_NEW_FORMAT) {
if (decode.new_stream) {
long rate;
int channels, enc;
MPG123(m, getformat, m->h, &rate, &channels, &enc);
LOG_INFO("setting track_start");
LOCK_O_not_direct;
output.next_sample_rate = decode_newstream(rate, output.supported_rates);
IF_DSD( output.next_fmt = PCM; )
output.track_start = outputbuf->writep;
if (output.fade_mode) _checkfade(true);
decode.new_stream = false;
UNLOCK_O_not_direct;
} else {
LOG_WARN("format change mid stream - not supported");
}
}
// expand 16bit output to 32bit samples
if (m->use16bit) {
s16_t *iptr;
s32_t *optr;
size_t count = size / 2;
size = count * 4;
iptr = (s16_t *)write_buf + count;
optr = (s32_t *)write_buf + count;
while (count--) {
*--optr = *--iptr << 16;
}
}
_buf_inc_readp(streambuf, bytes);
IF_DIRECT(
_buf_inc_writep(outputbuf, size);
);
IF_PROCESS(
process.in_frames = size / BYTES_PER_FRAME;
);
UNLOCK_O_direct;
LOG_SDEBUG("write %u frames", size / BYTES_PER_FRAME);
if (ret == MPG123_DONE || (bytes == 0 && size == 0 && stream.state <= DISCONNECT)) {
UNLOCK_S;
LOG_INFO("stream complete");
return DECODE_COMPLETE;
}
UNLOCK_S;
if (ret == MPG123_ERR) {
LOG_WARN("Error");
return DECODE_COMPLETE;
}
// OK and NEED_MORE keep running
return DECODE_RUNNING;
}
static void mpg_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
int err;
const long *list;
size_t count, i;
if (m->h) {
MPG123(m, delete, m->h);
}
m->h = MPG123(m, new, NULL, &err);
if (m->h == NULL) {
LOG_WARN("new error: %s", MPG123(m, plain_strerror, err));
}
// restrict output to 32bit or 16bit signed 2 channel based on library capability
MPG123(m, rates, &list, &count);
MPG123(m, format_none, m->h);
for (i = 0; i < count; i++) {
MPG123(m, format, m->h, list[i], 2, m->use16bit ? MPG123_ENC_SIGNED_16 : MPG123_ENC_SIGNED_32);
}
err = MPG123(m, open_feed, m->h);
if (err) {
LOG_WARN("open feed error: %s", MPG123(m, plain_strerror, err));
}
}
static void mpg_close(void) {
MPG123(m, delete, m->h);
m->h = NULL;
}
static bool load_mpg() {
#if !LINKALL
void *handle = dlopen(LIBMPG, RTLD_NOW);
char *err;
if (!handle) {
LOG_INFO("dlerror: %s", dlerror());
return false;
}
m->mpg123_init = dlsym(handle, "mpg123_init");
m->mpg123_feature = dlsym(handle, "mpg123_feature");
m->mpg123_rates = dlsym(handle, "mpg123_rates");
m->mpg123_format_none = dlsym(handle, "mpg123_format_none");
m->mpg123_format = dlsym(handle, "mpg123_format");
m->mpg123_new = dlsym(handle, "mpg123_new");
m->mpg123_delete = dlsym(handle, "mpg123_delete");
m->mpg123_open_feed = dlsym(handle, "mpg123_open_feed");
m->mpg123_decode = dlsym(handle, "mpg123_decode");
m->mpg123_getformat = dlsym(handle, "mpg123_getformat");
m->mpg123_plain_strerror = dlsym(handle, "mpg123_plain_strerror");
if ((err = dlerror()) != NULL) {
LOG_INFO("dlerror: %s", err);
return false;
}
LOG_INFO("loaded "LIBMPG);
#endif
return true;
}
struct codec *register_mpg(void) {
static struct codec ret = {
'm', // id
"mp3", // types
READ_SIZE, // min read
WRITE_SIZE, // min space
mpg_open, // open
mpg_close, // close
mpg_decode, // decode
};
m = malloc(sizeof(struct mpg));
if (!m) {
return NULL;
}
m->h = NULL;
if (!load_mpg()) {
return NULL;
}
MPG123(m, init);
m->use16bit = MPG123(m, feature, MPG123_FEATURE_OUTPUT_32BIT);
LOG_INFO("using mpg to decode mp3");
return &ret;
}