Package list squeezelite / f71dbca
version 1.3.1 - support for compile time linking of all libraries Adrian Smith 7 years ago
9 changed file(s) with 294 addition(s) and 153 deletion(s). Raw diff Collapse all Expand all
00 # Cross compile support - create a Makefile which defines these three variables and then includes this Makefile...
11 CFLAGS ?= -Wall -fPIC -O2 $(OPTS)
2 LDFLAGS ?= -lasound -lpthread -lm -ldl -lrt
2 LDFLAGS ?= -lasound -lpthread -lm -lrt
33 EXECUTABLE ?= squeezelite
44
55 SOURCES = main.c slimproto.c utils.c output.c buffer.c stream.c decode.c process.c resample.c flac.c pcm.c mad.c vorbis.c faad.c mpg.c ffmpeg.c
66 DEPS = squeezelite.h slimproto.h
7
8 UNAME = $(shell uname -s)
9
10 ifneq (,$(findstring -DLINKALL, $(CFLAGS)))
11 LDFLAGS += -lFLAC -lmad -lvorbisfile -lfaad -lmpg123
12 ifneq (,$(findstring -DFFMPEG, $(CFLAGS)))
13 LDFLAGS += -lavcodec -lavformat -lavutil
14 endif
15 ifneq (,$(findstring -DRESAMPLE, $(CFLAGS)))
16 LDFLAGS += -lsoxr
17 endif
18 else
19 # if not LINKALL and linux add -ldl
20 ifeq ($(UNAME), Linux)
21 LDFLAGS += -ldl
22 endif
23 endif
724
825 OBJECTS = $(SOURCES:.c=.o)
926
4242 bool empty;
4343 struct chunk_table *chunkinfo;
4444 // faad symbols to be dynamically loaded
45 #if !LINKALL
4546 NeAACDecConfigurationPtr (* NeAACDecGetCurrentConfiguration)(NeAACDecHandle);
4647 unsigned char (* NeAACDecSetConfiguration)(NeAACDecHandle, NeAACDecConfigurationPtr);
4748 NeAACDecHandle (* NeAACDecOpen)(void);
5051 char (* NeAACDecInit2)(NeAACDecHandle, unsigned char *pBuffer, unsigned long, unsigned long *, unsigned char *);
5152 void *(* NeAACDecDecode)(NeAACDecHandle, NeAACDecFrameInfo *, unsigned char *, unsigned long);
5253 char *(* NeAACDecGetErrorMessage)(unsigned char);
54 #endif
5355 };
5456
5557 static struct faad *a;
7981 #define IF_PROCESS(x)
8082 #endif
8183
84 #if LINKALL
85 #define NEAAC(h, fn, ...) (NeAACDec ## fn)(__VA_ARGS__)
86 #else
87 #define NEAAC(h, fn, ...) (h)->NeAACDec##fn(__VA_ARGS__)
88 #endif
89
8290 // minimal code for mp4 file parsing to extract audio config and find media data
8391
8492 // adapted from faad2/common/mp4ff
137145 return -1;
138146 }
139147 config_len = mp4_desc_length(&ptr);
140 if (a->NeAACDecInit2(a->hAac, ptr, config_len, samplerate_p, channels_p) == 0) {
148 if (NEAAC(a, Init2, a->hAac, ptr, config_len, samplerate_p, channels_p) == 0) {
141149 LOG_DEBUG("playable aac track: %u", trak);
142150 play = trak;
143151 }
343351 }
344352
345353 if (bytes_wrap >= 2) {
346 long n = a->NeAACDecInit(a->hAac, streambuf->readp, bytes_wrap, &samplerate, &channels);
354 long n = NEAAC(a, Init, a->hAac, streambuf->readp, bytes_wrap, &samplerate, &channels);
347355 if (n < 0) {
348356 found = -1;
349357 } else {
393401 memcpy(buf, streambuf->readp, bytes_wrap);
394402 memcpy(buf + bytes_wrap, streambuf->buf, WRAPBUF_LEN - bytes_wrap);
395403
396 iptr = a->NeAACDecDecode(a->hAac, &info, buf, WRAPBUF_LEN);
404 iptr = NEAAC(a, Decode, a->hAac, &info, buf, WRAPBUF_LEN);
397405
398406 } else {
399407
400 iptr = a->NeAACDecDecode(a->hAac, &info, streambuf->readp, bytes_wrap);
408 iptr = NEAAC(a, Decode, a->hAac, &info, streambuf->readp, bytes_wrap);
401409 }
402410
403411 if (info.error) {
404 LOG_WARN("error: %u %s", info.error, a->NeAACDecGetErrorMessage(info.error));
412 LOG_WARN("error: %u %s", info.error, NEAAC(a, GetErrorMessage, info.error));
405413 }
406414
407415 endstream = false;
546554 a->empty = false;
547555
548556 if (a->hAac) {
549 a->NeAACDecClose(a->hAac);
550 }
551 a->hAac = a->NeAACDecOpen();
552
553 conf = a->NeAACDecGetCurrentConfiguration(a->hAac);
557 NEAAC(a, Close, a->hAac);
558 }
559 a->hAac = NEAAC(a, Open);
560
561 conf = NEAAC(a, GetCurrentConfiguration, a->hAac);
554562
555563 conf->outputFormat = FAAD_FMT_24BIT;
556564 conf->downMatrix = 1;
557565
558 if (!a->NeAACDecSetConfiguration(a->hAac, conf)) {
566 if (!NEAAC(a, SetConfiguration, a->hAac, conf)) {
559567 LOG_WARN("error setting config");
560568 };
561569 }
562570
563571 static void faad_close(void) {
564 a->NeAACDecClose(a->hAac);
572 NEAAC(a, Close, a->hAac);
565573 a->hAac = NULL;
566574 if (a->chunkinfo) {
567575 free(a->chunkinfo);
574582 }
575583
576584 static bool load_faad() {
585 #if !LINKALL
577586 void *handle = dlopen(LIBFAAD, RTLD_NOW);
578587 char *err;
579588
582591 return false;
583592 }
584593
585 a = malloc(sizeof(struct faad));
586
587 a->hAac = NULL;
588 a->chunkinfo = NULL;
589 a->stsc = NULL;
590594 a->NeAACDecGetCurrentConfiguration = dlsym(handle, "NeAACDecGetCurrentConfiguration");
591595 a->NeAACDecSetConfiguration = dlsym(handle, "NeAACDecSetConfiguration");
592596 a->NeAACDecOpen = dlsym(handle, "NeAACDecOpen");
602606 }
603607
604608 LOG_INFO("loaded "LIBFAAD"");
609 #endif
610
605611 return true;
606612 }
607613
616622 faad_decode, // decode
617623 };
618624
625 a = malloc(sizeof(struct faad));
626 if (!a) {
627 return NULL;
628 }
629
630 a->hAac = NULL;
631 a->chunkinfo = NULL;
632 a->stsc = NULL;
633
619634 if (!load_faad()) {
620635 return NULL;
621636 }
4646 unsigned mmsh_bytes_left;
4747 unsigned mmsh_bytes_pad;
4848 unsigned mmsh_packet_len;
49 // library versions
50 unsigned avcodec_v, avformat_v, avutil_v;
49 #if !LINKALL
5150 // ffmpeg symbols to be dynamically loaded from libavcodec
5251 unsigned (* avcodec_version)(void);
5352 AVCodec * (* avcodec_find_decoder)(int);
5453 int attribute_align_arg (* avcodec_open2)(AVCodecContext *, const AVCodec *, AVDictionary **);
5554 AVFrame * (* avcodec_alloc_frame)(void);
56 void (* avcodec_free_frame)(AVFrame *);
55 void (* avcodec_free_frame)(AVFrame **);
5756 int attribute_align_arg (* avcodec_decode_audio4)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
5857 // ffmpeg symbols to be dynamically loaded from libavformat
5958 unsigned (* avformat_version)(void);
7473 void (* av_log_set_level)(int);
7574 int (* av_strerror)(int, char *, size_t);
7675 void * (* av_malloc)(size_t);
77 void (* av_free)(void *);
76 void (* av_freep)(void *);
77 #endif
7878 };
7979
8080 static struct ff_s *ff;
104104 #define IF_PROCESS(x)
105105 #endif
106106
107 #if LINKALL
108 #define AV(h, fn, ...) (av_ ## fn)(__VA_ARGS__)
109 #define AVIO(h, fn, ...) (avio_ ## fn)(__VA_ARGS__)
110 #define AVCODEC(h, fn, ...) (avcodec_ ## fn)(__VA_ARGS__)
111 #define AVFORMAT(h, fn, ...) (avformat_ ## fn)(__VA_ARGS__)
112 #else
113 #define AV(h, fn, ...) (h)->av_##fn(__VA_ARGS__)
114 #define AVIO(h, fn, ...) (h)->avio_##fn(__VA_ARGS__)
115 #define AVCODEC(h, fn, ...) (h)->avcodec_##fn(__VA_ARGS__)
116 #define AVFORMAT(h, fn, ...) (h)->avformat_##fn(__VA_ARGS__)
117 #endif
118
119
107120 // our own version of useful error function not included in earlier ffmpeg versions
108121 static char *av__err2str(errnum) {
109122 static char buf[64];
110 ff->av_strerror(errnum, buf, 64);
123 AV(ff, strerror, errnum, buf, 64);
111124 return buf;
112125 }
113126
250263 ff->mmsh_bytes_left = ff->mmsh_bytes_pad = ff->mmsh_packet_len = 0;
251264
252265 if (!ff->readbuf) {
253 ff->readbuf = ff->av_malloc(READ_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
254 }
255
256 avio = ff->avio_alloc_context(ff->readbuf, READ_SIZE, 0, NULL, _read_data, NULL, NULL);
266 ff->readbuf = AV(ff, malloc, READ_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
267 }
268
269 avio = AVIO(ff, alloc_context, ff->readbuf, READ_SIZE, 0, NULL, _read_data, NULL, NULL);
257270 avio->seekable = 0;
258271
259 ff->formatC = ff->avformat_alloc_context();
272 ff->formatC = AVFORMAT(ff, alloc_context);
260273 if (ff->formatC == NULL) {
261274 LOG_ERROR("null context");
262275 return DECODE_ERROR;
265278 ff->formatC->pb = avio;
266279 ff->formatC->flags |= AVFMT_FLAG_CUSTOM_IO | AVFMT_FLAG_NOPARSE;
267280
268 o = ff->avformat_open_input(&ff->formatC, "", ff->input_format, NULL);
281 o = AVFORMAT(ff, open_input, &ff->formatC, "", ff->input_format, NULL);
269282 if (o < 0) {
270283 LOG_WARN("avformat_open_input: %d %s", o, av__err2str(o));
271284 return DECODE_ERROR;
273286
274287 LOG_INFO("format: name:%s lname:%s", ff->formatC->iformat->name, ff->formatC->iformat->long_name);
275288
276 o = ff->avformat_find_stream_info(ff->formatC, NULL);
289 o = AVFORMAT(ff, find_stream_info, ff->formatC, NULL);
277290 if (o < 0) {
278291 LOG_WARN("avformat_find_stream_info: %d %s", o, av__err2str(o));
279292 return DECODE_ERROR;
306319
307320 ff->codecC = av_stream->codec;
308321
309 codec = ff->avcodec_find_decoder(ff->codecC->codec_id);
310
311 ff->avcodec_open2(ff->codecC, codec, NULL);
312
313 ff->frame = ff->avcodec_alloc_frame();
314
315 ff->avpkt = ff->av_malloc(sizeof(AVPacket));
322 codec = AVCODEC(ff, find_decoder, ff->codecC->codec_id);
323
324 AVCODEC(ff, open2, ff->codecC, codec, NULL);
325
326 ff->frame = AVCODEC(ff, alloc_frame);
327
328 ff->avpkt = AV(ff, malloc, sizeof(AVPacket));
316329 if (ff->avpkt == NULL) {
317330 LOG_ERROR("can't allocate avpkt");
318331 return DECODE_ERROR;
319332 }
320333
321 ff->av_init_packet(ff->avpkt);
334 AV(ff, init_packet, ff->avpkt);
322335 ff->avpkt->data = NULL;
323336 ff->avpkt->size = 0;
324337
333346
334347 got_frame = 0;
335348
336 if ((r = ff->av_read_frame(ff->formatC, ff->avpkt)) < 0) {
349 if ((r = AV(ff, read_frame, ff->formatC, ff->avpkt)) < 0) {
337350 if (r == AVERROR_EOF) {
338351 if (ff->end_of_stream) {
339352 LOG_INFO("decode complete");
357370
358371 while (pkt_c.size > 0 || got_frame) {
359372
360 len = ff->avcodec_decode_audio4(ff->codecC, ff->frame, &got_frame, &pkt_c);
373 len = AVCODEC(ff, decode_audio4, ff->codecC, ff->frame, &got_frame, &pkt_c);
361374 if (len < 0) {
362375 LOG_ERROR("avcodec_decode_audio4 error: %i %s", len, av__err2str(len));
363376 return DECODE_RUNNING;
490503 }
491504 }
492505
493 ff->av_free_packet(ff->avpkt);
506 AV(ff, free_packet, ff->avpkt);
494507
495508 return DECODE_RUNNING;
496509 }
497510
498511 static void _free_ff_data(void) {
499512 if (ff->formatC) {
500 if (ff->formatC->pb) ff->av_free(ff->formatC->pb);
501 ff->avformat_free_context(ff->formatC);
513 if (ff->formatC->pb) AV(ff, freep, &ff->formatC->pb);
514 AVFORMAT(ff, free_context, ff->formatC);
502515 ff->formatC = NULL;
503516 }
504517
505518 if (ff->frame) {
506519 // ffmpeg version dependant free function
507 ff->avcodec_free_frame ? ff->avcodec_free_frame(ff->frame) : ff->av_free(ff->frame);
520 #if !LINKALL
521 ff->avcodec_free_frame ? AVCODEC(ff, free_frame, &ff->frame) : AV(ff, freep, &ff->frame);
522 #elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,28,0)
523 AVCODEC(ff, free_frame, &ff->frame);
524 #else
525 AV(ff, freep, &ff->frame);
526 #endif
508527 ff->frame = NULL;
509528 }
510529
511530 if (ff->avpkt) {
512 ff->av_free_packet(ff->avpkt);
513 ff->av_free(ff->avpkt);
531 AV(ff, free_packet, ff->avpkt);
532 AV(ff, freep, &ff->avpkt);
514533 ff->avpkt = NULL;
515534 }
516535 }
518537 static void ff_open_wma(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
519538 _free_ff_data();
520539
521 ff->input_format = ff->av_find_input_format("asf");
540 ff->input_format = AV(ff, find_input_format, "asf");
522541 if (ff->input_format == NULL) {
523542 LOG_ERROR("asf format not supported by ffmpeg library");
524543 }
534553 static void ff_open_alac(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
535554 _free_ff_data();
536555
537 ff->input_format = ff->av_find_input_format("mp4");
556 ff->input_format = AV(ff, find_input_format, "mp4");
538557 if (ff->input_format == NULL) {
539558 LOG_ERROR("mp4 format not supported by ffmpeg library");
540559 }
549568 _free_ff_data();
550569
551570 if (ff->readbuf) {
552 ff->av_free(ff->readbuf);
571 AV(ff, freep, &ff->readbuf);
553572 ff->readbuf = NULL;
554573 }
555574 }
556575
557576 static bool load_ff() {
577 #if !LINKALL
558578 void *handle_codec = NULL, *handle_format = NULL, *handle_util = NULL;
559579 char name[30];
560580 char *err;
581601 LOG_INFO("dlerror: %s", dlerror());
582602 return false;
583603 }
584
585 ff = malloc(sizeof(struct ff_s));
586 memset(ff, 0, sizeof(struct ff_s));
587604
588605 ff->avcodec_version = dlsym(handle_codec, "avcodec_version");
589606 ff->avcodec_find_decoder = dlsym(handle_codec, "avcodec_find_decoder");
599616 return false;
600617 }
601618
602 ff->avcodec_v = ff->avcodec_version();
603 LOG_INFO("loaded "LIBAVCODEC" (%u.%u.%u)", LIBAVCODEC_VERSION_MAJOR, ff->avcodec_v >> 16, (ff->avcodec_v >> 8) & 0xff, ff->avcodec_v & 0xff);
619 LOG_INFO("loaded "LIBAVCODEC" (%u.%u.%u)", LIBAVCODEC_VERSION_MAJOR, ff->avcodec_version() >> 16, (ff->avcodec_version() >> 8) & 0xff, ff->avcodec_version() & 0xff);
604620
605621 ff->avformat_version = dlsym(handle_format, "avformat_version");
606622 ff->avformat_alloc_context = dlsym(handle_format, "avformat_alloc_context");
617633 return false;
618634 }
619635
620 ff->avformat_v = ff->avformat_version();
621 LOG_INFO("loaded "LIBAVFORMAT" (%u.%u.%u)", LIBAVFORMAT_VERSION_MAJOR, ff->avformat_v >> 16, (ff->avformat_v >> 8) & 0xff, ff->avformat_v & 0xff);
636 LOG_INFO("loaded "LIBAVFORMAT" (%u.%u.%u)", LIBAVFORMAT_VERSION_MAJOR, ff->avformat_version() >> 16, (ff->avformat_version() >> 8) & 0xff, ff->avformat_version() & 0xff);
622637
623638 ff->avutil_version = dlsym(handle_util, "avutil_version");
624639 ff->av_log_set_callback = dlsym(handle_util, "av_log_set_callback");
625640 ff->av_log_set_level = dlsym(handle_util, "av_log_set_level");
626641 ff->av_strerror = dlsym(handle_util, "av_strerror");
627642 ff->av_malloc = dlsym(handle_util, "av_malloc");
628 ff->av_free = dlsym(handle_util, "av_free");
643 ff->av_freep = dlsym(handle_util, "av_freep");
629644
630645 if ((err = dlerror()) != NULL) {
631646 LOG_INFO("dlerror: %s", err);
632647 return false;
633648 }
634649
635 ff->avutil_v = ff->avutil_version();
636 LOG_INFO("loaded "LIBAVUTIL" (%u.%u.%u)", LIBAVUTIL_VERSION_MAJOR, ff->avutil_v >> 16, (ff->avutil_v >> 8) & 0xff, ff->avutil_v & 0xff);
650 LOG_INFO("loaded "LIBAVUTIL" (%u.%u.%u)", LIBAVUTIL_VERSION_MAJOR, ff->avutil_version() >> 16, (ff->avutil_version() >> 8) & 0xff, ff->avutil_version() & 0xff);
651
652 #endif
637653
638654 return true;
639655 }
651667
652668 struct codec *register_ff(const char *codec) {
653669 if (!registered) {
670
671 ff = malloc(sizeof(struct ff_s));
672 if (!ff) {
673 return NULL;
674 }
675
676 memset(ff, 0, sizeof(struct ff_s));
654677
655678 if (!load_ff()) {
656679 return NULL;
668691 default: break;
669692 }
670693
671 ff->av_log_set_callback(av_err_callback);
672
673 ff->av_register_all();
694 AV(ff, log_set_callback, av_err_callback);
695
696 AV(ff, register_all);
674697
675698 registered = true;
676699 }
2323
2424 struct flac {
2525 FLAC__StreamDecoder *decoder;
26 #if !LINKALL
2627 // FLAC symbols to be dynamically loaded
2728 const char **FLAC__StreamDecoderErrorStatusString;
2829 const char **FLAC__StreamDecoderStateString;
4344 );
4445 FLAC__bool (* FLAC__stream_decoder_process_single)(FLAC__StreamDecoder *decoder);
4546 FLAC__StreamDecoderState (* FLAC__stream_decoder_get_state)(const FLAC__StreamDecoder *decoder);
47 #endif
4648 };
4749
4850 static struct flac *f;
7274 #define IF_PROCESS(x)
7375 #endif
7476
77 #if LINKALL
78 #define FLAC(h, fn, ...) (FLAC__ ## fn)(__VA_ARGS__)
79 #define FLAC_A(h, a) (FLAC__ ## a)
80 #else
81 #define FLAC(h, fn, ...) (h)->FLAC__##fn(__VA_ARGS__)
82 #define FLAC_A(h, a) (h)->FLAC__ ## a
83 #endif
84
7585 static FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *want, void *client_data) {
7686 size_t bytes;
7787 bool end;
171181 }
172182
173183 static void error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
174 LOG_INFO("flac error: %s", f->FLAC__StreamDecoderErrorStatusString[status]);
184 LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderErrorStatusString)[status]);
175185 }
176186
177187 static void flac_open(u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness) {
178188 if (f->decoder) {
179 f->FLAC__stream_decoder_reset(f->decoder);
189 FLAC(f, stream_decoder_reset, f->decoder);
180190 } else {
181 f->decoder = f->FLAC__stream_decoder_new();
182 }
183 f->FLAC__stream_decoder_init_stream(f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
191 f->decoder = FLAC(f, stream_decoder_new);
192 }
193 FLAC(f, stream_decoder_init_stream, f->decoder, &read_cb, NULL, NULL, NULL, NULL, &write_cb, NULL, &error_cb, NULL);
184194 }
185195
186196 static void flac_close(void) {
187 f->FLAC__stream_decoder_delete(f->decoder);
197 FLAC(f, stream_decoder_delete, f->decoder);
188198 f->decoder = NULL;
189199 }
190200
191201 static decode_state flac_decode(void) {
192 bool ok = f->FLAC__stream_decoder_process_single(f->decoder);
193 FLAC__StreamDecoderState state = f->FLAC__stream_decoder_get_state(f->decoder);
202 bool ok = FLAC(f, stream_decoder_process_single, f->decoder);
203 FLAC__StreamDecoderState state = FLAC(f, stream_decoder_get_state, f->decoder);
194204
195205 if (!ok && state != FLAC__STREAM_DECODER_END_OF_STREAM) {
196 LOG_INFO("flac error: %s", f->FLAC__StreamDecoderStateString[state]);
206 LOG_INFO("flac error: %s", FLAC_A(f, StreamDecoderStateString)[state]);
197207 };
198208
199209 if (state == FLAC__STREAM_DECODER_END_OF_STREAM) {
206216 }
207217
208218 static bool load_flac() {
219 #if !LINKALL
209220 void *handle = dlopen(LIBFLAC, RTLD_NOW);
210221 char *err;
211222
214225 return false;
215226 }
216227
217 f = malloc(sizeof(struct flac));
218
219 f->decoder = NULL;
220228 f->FLAC__StreamDecoderErrorStatusString = dlsym(handle, "FLAC__StreamDecoderErrorStatusString");
221229 f->FLAC__StreamDecoderStateString = dlsym(handle, "FLAC__StreamDecoderStateString");
222230 f->FLAC__stream_decoder_new = dlsym(handle, "FLAC__stream_decoder_new");
232240 }
233241
234242 LOG_INFO("loaded "LIBFLAC);
243 #endif
244
235245 return true;
236246 }
237247
246256 flac_decode, // decode
247257 };
248258
259 f = malloc(sizeof(struct flac));
260 if (!f) {
261 return NULL;
262 }
263
264 f->decoder = NULL;
265
249266 if (!load_flac()) {
250267 return NULL;
251268 }
+30
-15
mad.c less more
3737 u32_t skip;
3838 u64_t samples;
3939 u32_t padding;
40 #if !LINKALL
4041 // mad symbols to be dynamically loaded
4142 void (* mad_stream_init)(struct mad_stream *);
4243 void (* mad_frame_init)(struct mad_frame *);
4748 int (* mad_frame_decode)(struct mad_frame *, struct mad_stream *);
4849 void (* mad_synth_frame)(struct mad_synth *, struct mad_frame const *);
4950 char const *(* mad_stream_errorstr)(struct mad_stream const *);
51 #endif
5052 };
5153
5254 static struct mad *m;
7678 #define IF_PROCESS(x)
7779 #endif
7880
81 #if LINKALL
82 #define MAD(h, fn, ...) (mad_ ## fn)(__VA_ARGS__)
83 #else
84 #define MAD(h, fn, ...) (h)->mad_##fn(__VA_ARGS__)
85 #endif
86
7987 // based on libmad minimad.c scale
8088 static inline u32_t scale(mad_fixed_t sample) {
8189 sample += (1L << (MAD_F_FRACBITS - 24));
167175
168176 UNLOCK_S;
169177
170 m->mad_stream_buffer(&m->stream, m->readbuf, m->readbuf_len);
178 MAD(m, stream_buffer, &m->stream, m->readbuf, m->readbuf_len);
171179
172180 while (true) {
173181 size_t frames;
175183 s32_t *iptrr;
176184 unsigned max_frames;
177185
178 if (m->mad_frame_decode(&m->frame, &m->stream) == -1) {
186 if (MAD(m, frame_decode, &m->frame, &m->stream) == -1) {
179187 decode_state ret;
180188 if (!eos && m->stream.error == MAD_ERROR_BUFLEN) {
181189 ret = DECODE_RUNNING;
182190 } else if (eos && (m->stream.error == MAD_ERROR_BUFLEN || m->stream.error == MAD_ERROR_LOSTSYNC)) {
183191 ret = DECODE_COMPLETE;
184192 } else if (!MAD_RECOVERABLE(m->stream.error)) {
185 LOG_INFO("mad_frame_decode error: %s - stopping decoder", m->mad_stream_errorstr(&m->stream));
193 LOG_INFO("mad_frame_decode error: %s - stopping decoder", MAD(m, stream_errorstr, &m->stream));
186194 ret = DECODE_COMPLETE;
187195 } else {
188196 if (m->stream.error != m->last_error) {
189197 // suppress repeat error messages
190 LOG_DEBUG("mad_frame_decode error: %s", m->mad_stream_errorstr(&m->stream));
198 LOG_DEBUG("mad_frame_decode error: %s", MAD(m, stream_errorstr, &m->stream));
191199 }
192200 ret = DECODE_RUNNING;
193201 }
195203 return ret;
196204 };
197205
198 m->mad_synth_frame(&m->synth, &m->frame);
206 MAD(m, synth_frame, &m->synth, &m->frame);
199207
200208 if (decode.new_stream) {
201209 LOCK_O;
300308 m->samples = 0;
301309 m->readbuf_len = 0;
302310 m->last_error = MAD_ERROR_NONE;
303 m->mad_stream_init(&m->stream);
304 m->mad_frame_init(&m->frame);
305 m->mad_synth_init(&m->synth);
311 MAD(m, stream_init, &m->stream);
312 MAD(m, frame_init, &m->frame);
313 MAD(m, synth_init, &m->synth);
306314 }
307315
308316 static void mad_close(void) {
309 mad_synth_finish(&m->synth);
310 m->mad_frame_finish(&m->frame);
311 m->mad_stream_finish(&m->stream);
317 mad_synth_finish(&m->synth); // macro only in current version
318 MAD(m, frame_finish, &m->frame);
319 MAD(m, stream_finish, &m->stream);
312320 free(m->readbuf);
313321 m->readbuf = NULL;
314322 }
315323
316324 static bool load_mad() {
325 #if !LINKALL
317326 void *handle = dlopen(LIBMAD, RTLD_NOW);
318327 char *err;
319328
322331 return false;
323332 }
324333
325 m = malloc(sizeof(struct mad));
326
327 m->readbuf = NULL;
328 m->readbuf_len = 0;
329334 m->mad_stream_init = dlsym(handle, "mad_stream_init");
330335 m->mad_frame_init = dlsym(handle, "mad_frame_init");
331336 m->mad_synth_init = dlsym(handle, "mad_synth_init");
342347 }
343348
344349 LOG_INFO("loaded "LIBMAD);
350 #endif
351
345352 return true;
346353 }
347354
356363 mad_decode, // decode
357364 };
358365
366 m = malloc(sizeof(struct mad));
367 if (!m) {
368 return NULL;
369 }
370
371 m->readbuf = NULL;
372 m->readbuf_len = 0;
373
359374 if (!load_mad()) {
360375 return NULL;
361376 }
+37
-22
mpg.c less more
2727 struct mpg {
2828 mpg123_handle *h;
2929 bool use16bit;
30 #if !LINKALL
3031 // mpg symbols to be dynamically loaded
3132 int (* mpg123_init)(void);
3233 int (* mpg123_feature)(const enum mpg123_feature_set);
3940 int (* mpg123_decode)(mpg123_handle *, const unsigned char *, size_t, unsigned char *, size_t, size_t *);
4041 int (* mpg123_getformat)(mpg123_handle *, long *, int *, int *);
4142 const char* (* mpg123_plain_strerror)(int);
43 #endif
4244 };
4345
4446 static struct mpg *m;
7274 #define IF_PROCESS(x)
7375 #endif
7476
77 #if LINKALL
78 #define MPG123(h, fn, ...) (mpg123_ ## fn)(__VA_ARGS__)
79 #else
80 #define MPG123(h, fn, ...) (h)->mpg123_##fn(__VA_ARGS__)
81 #endif
82
7583 static decode_state mpg_decode(void) {
7684 size_t bytes, space, size;
7785 int ret;
108116 space = 0;
109117 }
110118
111 ret = m->mpg123_decode(m->h, streambuf->readp, bytes, write_buf, space, &size);
119 ret = MPG123(m, decode, m->h, streambuf->readp, bytes, write_buf, space, &size);
112120
113121 if (ret == MPG123_NEW_FORMAT) {
114122
116124 long rate;
117125 int channels, enc;
118126
119 m->mpg123_getformat(m->h, &rate, &channels, &enc);
127 MPG123(m, getformat, m->h, &rate, &channels, &enc);
120128
121129 LOG_INFO("setting track_start");
122130 LOCK_O_not_direct;
178186 size_t count, i;
179187
180188 if (m->h) {
181 m->mpg123_delete(m->h);
182 }
183
184 m->h = m->mpg123_new(NULL, &err);
189 MPG123(m, delete, m->h);
190 }
191
192 m->h = MPG123(m, new, NULL, &err);
185193
186194 if (m->h == NULL) {
187 LOG_WARN("new error: %s", m->mpg123_plain_strerror(err));
195 LOG_WARN("new error: %s", MPG123(m, plain_strerror, err));
188196 }
189197
190198 // restrict output to 32bit or 16bit signed 2 channel based on library capability
191 m->mpg123_rates(&list, &count);
192 m->mpg123_format_none(m->h);
199 MPG123(m, rates, &list, &count);
200 MPG123(m, format_none, m->h);
193201 for (i = 0; i < count; i++) {
194 m->mpg123_format(m->h, list[i], 2, m->use16bit ? MPG123_ENC_SIGNED_16 : MPG123_ENC_SIGNED_32);
195 }
196
197 err = m->mpg123_open_feed(m->h);
202 MPG123(m, format, m->h, list[i], 2, m->use16bit ? MPG123_ENC_SIGNED_16 : MPG123_ENC_SIGNED_32);
203 }
204
205 err = MPG123(m, open_feed, m->h);
198206
199207 if (err) {
200 LOG_WARN("open feed error: %s", m->mpg123_plain_strerror(err));
208 LOG_WARN("open feed error: %s", MPG123(m, plain_strerror, err));
201209 }
202210 }
203211
204212 static void mpg_close(void) {
205 m->mpg123_delete(m->h);
213 MPG123(m, delete, m->h);
206214 m->h = NULL;
207215 }
208216
209217 static bool load_mpg() {
218 #if !LINKALL
210219 void *handle = dlopen(LIBMPG, RTLD_NOW);
211220 char *err;
212221
215224 return false;
216225 }
217226
218 m = malloc(sizeof(struct mpg));
219
220 m->h = NULL;
221227 m->mpg123_init = dlsym(handle, "mpg123_init");
222228 m->mpg123_feature = dlsym(handle, "mpg123_feature");
223229 m->mpg123_rates = dlsym(handle, "mpg123_rates");
235241 return false;
236242 }
237243
238 m->mpg123_init();
239
240 m->use16bit = !m->mpg123_feature(MPG123_FEATURE_OUTPUT_32BIT);
241
242244 LOG_INFO("loaded "LIBMPG);
245 #endif
246
243247 return true;
244248 }
245249
254258 mpg_decode, // decode
255259 };
256260
261 m = malloc(sizeof(struct mpg));
262 if (!m) {
263 return NULL;
264 }
265
266 m->h = NULL;
267
257268 if (!load_mpg()) {
258269 return NULL;
259270 }
260271
272 MPG123(m, init);
273
274 m->use16bit = MPG123(m, feature, MPG123_FEATURE_OUTPUT_32BIT);
275
261276 return &ret;
262277 }
2626 #include <math.h>
2727 #include <soxr.h>
2828
29 #if LINUX
30 #define LIBSOXR "libsoxr.so.0"
31 #endif
32 #if OSX
33 #define LIBSOXR "libsoxr.0.dylib"
34 #endif
35 #if WIN
36 #define LIBSOXR "libsoxr.dll"
37 #endif
38
3929 extern log_level loglevel;
4030
4131 struct soxr {
4838 double q_passband_end; /* 0dB pt. bandwidth to preserve; nyquist=1 0.913 */
4939 double q_stopband_begin; /* Aliasing/imaging control; > passband_end 1 */
5040 double scale;
41 bool max_rate;
42 #if !LINKALL
5143 // soxr symbols to be dynamically loaded
5244 soxr_io_spec_t (* soxr_io_spec)(soxr_datatype_t itype, soxr_datatype_t otype);
5345 soxr_quality_spec_t (* soxr_quality_spec)(unsigned long recipe, unsigned long flags);
5648 void (* soxr_delete)(soxr_t);
5749 soxr_error_t (* soxr_process)(soxr_t, soxr_in_t, size_t, size_t *, soxr_out_t, size_t olen, size_t *);
5850 size_t *(* soxr_num_clips)(soxr_t);
59 bool max_rate;
51 // soxr_strerror is a macro so not included here
52 #endif
6053 };
6154
6255 static struct soxr *r;
56
57 #if LINKALL
58 #define SOXR(h, fn, ...) (soxr_ ## fn)(__VA_ARGS__)
59 #else
60 #define SOXR(h, fn, ...) (h)->soxr_##fn(__VA_ARGS__)
61 #endif
6362
6463
6564 void resample_samples(struct processstate *process) {
6766 size_t clip_cnt;
6867
6968 soxr_error_t error =
70 r->soxr_process(r->resampler, process->inbuf, process->in_frames, &idone, process->outbuf, process->max_out_frames, &odone);
69 SOXR(r, process, r->resampler, process->inbuf, process->in_frames, &idone, process->outbuf, process->max_out_frames, &odone);
7170 if (error) {
7271 LOG_INFO("soxr_process error: %s", soxr_strerror(error));
7372 }
8281 process->total_in += idone;
8382 process->total_out += odone;
8483
85 clip_cnt = *(r->soxr_num_clips(r->resampler));
84 clip_cnt = *(SOXR(r, num_clips, r->resampler));
8685 if (clip_cnt - r->old_clips) {
8786 LOG_DEBUG("resampling clips: %u", (unsigned)(clip_cnt - r->old_clips));
8887 r->old_clips = clip_cnt;
9392 size_t odone;
9493 size_t clip_cnt;
9594
96 soxr_error_t error = r->soxr_process(r->resampler, NULL, 0, NULL, process->outbuf, process->max_out_frames, &odone);
95 soxr_error_t error = SOXR(r, process, r->resampler, NULL, 0, NULL, process->outbuf, process->max_out_frames, &odone);
9796 if (error) {
9897 LOG_INFO("soxr_process error: %s", soxr_strerror(error));
9998 }
101100 process->out_frames = odone;
102101 process->total_out += odone;
103102
104 clip_cnt = *(r->soxr_num_clips(r->resampler));
103 clip_cnt = *(SOXR(r, num_clips, r->resampler));
105104 if (clip_cnt - r->old_clips) {
106105 LOG_DEBUG("resampling clips: %u", (unsigned)(clip_cnt - r->old_clips));
107106 r->old_clips = clip_cnt;
111110
112111 LOG_INFO("resample track complete - total track clips: %u", r->old_clips);
113112
114 r->soxr_delete(r->resampler);
113 SOXR(r, delete, r->resampler);
115114 r->resampler = NULL;
116115
117116 return true;
137136 process->out_sample_rate = outrate;
138137
139138 if (r->resampler) {
140 r->soxr_delete(r->resampler);
139 SOXR(r, delete, r->resampler);
141140 r->resampler = NULL;
142141 }
143142
149148
150149 LOG_INFO("resampling from %u -> %u", raw_sample_rate, outrate);
151150
152 io_spec = r->soxr_io_spec(SOXR_INT32_I, SOXR_INT32_I);
151 io_spec = SOXR(r, io_spec, SOXR_INT32_I, SOXR_INT32_I);
153152 io_spec.scale = r->scale;
154153
155 q_spec = r->soxr_quality_spec(r->q_recipe, r->q_flags);
154 q_spec = SOXR(r, quality_spec, r->q_recipe, r->q_flags);
156155 if (r->q_precision > 0) {
157156 q_spec.precision = r->q_precision;
158157 }
170169 "phase_response: %03.1f, flags: 0x%02x], soxr_io_spec_t[scale: %03.2f]", q_spec.precision,
171170 q_spec.passband_end, q_spec.stopband_begin, q_spec.phase_response, q_spec.flags, io_spec.scale);
172171
173 r->resampler = r->soxr_create(raw_sample_rate, outrate, 2, &error, &io_spec, &q_spec, NULL);
172 r->resampler = SOXR(r, create, raw_sample_rate, outrate, 2, &error, &io_spec, &q_spec, NULL);
174173 if (error) {
175174 LOG_INFO("soxr_create error: %s", soxr_strerror(error));
176175 return false;
188187
189188 void resample_flush(void) {
190189 if (r->resampler) {
191 r->soxr_delete(r->resampler);
190 SOXR(r, delete, r->resampler);
192191 r->resampler = NULL;
193192 }
194193 }
195194
196195 static bool load_soxr(void) {
196 #if !LINKALL
197197 void *handle = dlopen(LIBSOXR, RTLD_NOW);
198198 char *err;
199199
202202 return false;
203203 }
204204
205 r = malloc(sizeof(struct soxr));
206
207 r->resampler = NULL;
208 r->old_clips = 0;
209 r->max_rate = false;
210205 r->soxr_io_spec = dlsym(handle, "soxr_io_spec");
211206 r->soxr_quality_spec = dlsym(handle, "soxr_quality_spec");
212207 r->soxr_create = dlsym(handle, "soxr_create");
220215 }
221216
222217 LOG_INFO("loaded "LIBSOXR);
218 #endif
219
223220 return true;
224221 }
225222
227224 char *recipe = NULL, *flags = NULL;
228225 char *atten = NULL;
229226 char *precision = NULL, *passband_end = NULL, *stopband_begin = NULL, *phase_response = NULL;
227
228 r = malloc(sizeof(struct soxr));
229 if (!r) {
230 LOG_WARN("resampling disabled");
231 return false;
232 }
233
234 r->resampler = NULL;
235 r->old_clips = 0;
236 r->max_rate = false;
230237
231238 if (!load_soxr()) {
232239 LOG_WARN("resampling disabled");
1919
2020 // make may define: PORTAUDIO, SELFPIPE or RESAMPLE to influence build
2121
22 #define VERSION "v1.3"
22 #define VERSION "v1.3.1"
2323
2424 // build detection
2525 #if defined(linux)
7575 #undef FFMPEG
7676 #define FFMPEG 1
7777 #else
78 #undef FFMPEG
7978 #define FFMPEG 0
8079 #endif
8180
8382 #undef VISEXPORT
8483 #define VISEXPORT 1 // visulizer export support uses linux shared memory
8584 #else
86 #undef VISEXPORT
8785 #define VISEXPORT 0
8886 #endif
8987
90 // dynamically loaded libraries
88 #if defined(LINKALL)
89 #undef LINKALL
90 #define LINKALL 1 // link all libraries at build time - requires all to be available at run time
91 #else
92 #define LINKALL 0
93 #endif
94
95
96 #if !LINKALL
97
98 // dynamically loaded libraries at run time
9199 #if LINUX
92100 #define LIBFLAC "libFLAC.so.8"
93101 #define LIBMAD "libmad.so.0"
98106 #define LIBAVUTIL "libavutil.so.%d"
99107 #define LIBAVCODEC "libavcodec.so.%d"
100108 #define LIBAVFORMAT "libavformat.so.%d"
109 #define LIBSOXR "libsoxr.so.0"
101110 #endif
102111
103112 #if OSX
110119 #define LIBAVUTIL "libavutil.%d.dylib"
111120 #define LIBAVCODEC "libavcodec.%d.dylib"
112121 #define LIBAVFORMAT "libavformat.%d.dylib"
122 #define LIBSOXR "libsoxr.0.dylib"
113123 #endif
114124
115125 #if WIN
122132 #define LIBAVUTIL "avutil-%d.dll"
123133 #define LIBAVCODEC "avcodec-%d.dll"
124134 #define LIBAVFORMAT "avformat-%d.dll"
125 #endif
135 #define LIBSOXR "libsoxr.dll"
136 #endif
137
138 #endif // !LINKALL
126139
127140 // config options
128141 #define STREAMBUF_SIZE (2 * 1024 * 1024)
3030
3131 struct vorbis {
3232 OggVorbis_File *vf;
33 #if !LINKALL
3334 // vorbis symbols to be dynamically loaded - from either vorbisfile or vorbisidec (tremor) version of library
3435 vorbis_info *(* ov_info)(OggVorbis_File *vf, int link);
3536 int (* ov_clear)(OggVorbis_File *vf);
3637 long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
3738 long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
3839 int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
40 #endif
3941 };
4042
4143 static struct vorbis *v;
6971 #define IF_PROCESS(x)
7072 #endif
7173
74 #if LINKALL
75 #define OV(h, fn, ...) (ov_ ## fn)(__VA_ARGS__)
76 #define TREMOR(h) 0
77 extern int ov_read_tremor(); // needed to enable compilation, not linked
78 #else
79 #define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
80 #define TREMOR(h) (h)->ov_read_tremor
81 #endif
82
7283 // called with mutex locked within vorbis_decode to avoid locking O before S
7384 static size_t _read_cb(void *ptr, size_t size, size_t nmemb, void *datasource) {
7485 size_t bytes;
118129
119130 cbs.read_func = _read_cb;
120131
121 if (v->ov_read_tremor) {
132 if (TREMOR(v)) {
122133 cbs.seek_func = _seek_cb; cbs.close_func = _close_cb; cbs.tell_func = _tell_cb;
123134 } else {
124135 cbs.seek_func = NULL; cbs.close_func = NULL; cbs.tell_func = NULL;
125136 }
126137
127 if ((err = v->ov_open_callbacks(streambuf, v->vf, NULL, 0, cbs)) < 0) {
138 if ((err = OV(v, open_callbacks, streambuf, v->vf, NULL, 0, cbs)) < 0) {
128139 LOG_WARN("open_callbacks error: %d", err);
129140 UNLOCK_O_direct;
130141 UNLOCK_S;
131142 return DECODE_COMPLETE;
132143 }
133144
134 info = v->ov_info(v->vf, -1);
145 info = OV(v, info, v->vf, -1);
135146
136147 LOG_INFO("setting track_start");
137148 LOCK_O_not_direct;
165176 );
166177
167178 // write the decoded frames into outputbuf even though they are 16 bits per sample, then unpack them
168 if (v->ov_read) {
179 if (!TREMOR(v)) {
169180 #if SL_LITTLE_ENDIAN
170 n = v->ov_read(v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
181 n = OV(v, read, v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
171182 #else
172 n = v->ov_read(v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
183 n = OV(v, read, v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
173184 #endif
174185 } else {
175 n = v->ov_read_tremor(v->vf, (char *)write_buf, bytes, &s);
186 n = OV(v, read_tremor, v->vf, (char *)write_buf, bytes, &s);
176187 }
177188
178189 if (n > 0) {
233244 if (!v->vf) {
234245 v->vf = malloc(sizeof(OggVorbis_File) + 128); // add some padding as struct size may be larger
235246 } else {
236 v->ov_clear(v->vf);
247 OV(v, clear, v->vf);
237248 }
238249 }
239250
240251 static void vorbis_close(void) {
241 v->ov_clear(v->vf);
252 OV(v, clear, v->vf);
242253 free(v->vf);
243254 v->vf = NULL;
244255 }
245256
246257 static bool load_vorbis() {
258 #if !LINKALL
247259 void *handle = dlopen(LIBVORBIS, RTLD_NOW);
248260 char *err;
249261 bool tremor = false;
258270 }
259271 }
260272
261 v = malloc(sizeof(struct vorbis));
262 v->vf = NULL;
263273 v->ov_read = tremor ? NULL : dlsym(handle, "ov_read");
264274 v->ov_read_tremor = tremor ? dlsym(handle, "ov_read") : NULL;
265275 v->ov_info = dlsym(handle, "ov_info");
272282 }
273283
274284 LOG_INFO("loaded %s", tremor ? LIBTREMOR : LIBVORBIS);
285 #endif
286
275287 return true;
276288 }
277289
286298 vorbis_decode,// decode
287299 };
288300
301 v = malloc(sizeof(struct vorbis));
302 if (!v) {
303 return NULL;
304 }
305
306 v->vf = NULL;
307
289308 if (!load_vorbis()) {
290309 return NULL;
291310 }