Merge branch 'master-v4.3' of https://github.com/sle118/squeezelite-esp32 into master-v4.3

This commit is contained in:
Sebastien L
2023-04-07 10:53:29 -04:00
12 changed files with 362 additions and 241 deletions

View File

@@ -136,7 +136,7 @@ const static actrls_t controls = {
NULL, NULL, // rew, fwd
bt_prev, bt_next, // prev, next
NULL, NULL, NULL, NULL, // left, right, up, down
NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // pre1-10
bt_volume_down, bt_volume_up, bt_toggle// knob left, knob_right, knob push
};

View File

@@ -90,7 +90,7 @@ const static actrls_t controls = {
NULL, NULL, // rew, fwd
raop_prev, raop_next, // prev, next
NULL, NULL, NULL, NULL, // left, right, up, down
NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // pre1-10
raop_volume_down, raop_volume_up, raop_toggle// knob left, knob_right, knob push
};

View File

@@ -60,7 +60,7 @@ static const actrls_config_map_t actrls_config_map[] =
static const char * actrls_action_s[ ] = { EP(ACTRLS_POWER),EP(ACTRLS_VOLUP),EP(ACTRLS_VOLDOWN),EP(ACTRLS_TOGGLE),EP(ACTRLS_PLAY),
EP(ACTRLS_PAUSE),EP(ACTRLS_STOP),EP(ACTRLS_REW),EP(ACTRLS_FWD),EP(ACTRLS_PREV),EP(ACTRLS_NEXT),
EP(BCTRLS_UP),EP(BCTRLS_DOWN),EP(BCTRLS_LEFT),EP(BCTRLS_RIGHT),
EP(BCTRLS_PS1),EP(BCTRLS_PS2),EP(BCTRLS_PS3),EP(BCTRLS_PS4),EP(BCTRLS_PS5),EP(BCTRLS_PS6),EP(BCTRLS_PS7),EP(BCTRLS_PS8),EP(BCTRLS_PS9),EP(BCTRLS_PS10),
EP(BCTRLS_PS0),EP(BCTRLS_PS1),EP(BCTRLS_PS2),EP(BCTRLS_PS3),EP(BCTRLS_PS4),EP(BCTRLS_PS5),EP(BCTRLS_PS6),EP(BCTRLS_PS7),EP(BCTRLS_PS8),EP(BCTRLS_PS9),
EP(KNOB_LEFT),EP(KNOB_RIGHT),EP(KNOB_PUSH),
""} ;

View File

@@ -14,7 +14,7 @@
typedef enum { ACTRLS_NONE = -1, ACTRLS_POWER,ACTRLS_VOLUP, ACTRLS_VOLDOWN, ACTRLS_TOGGLE, ACTRLS_PLAY,
ACTRLS_PAUSE, ACTRLS_STOP, ACTRLS_REW, ACTRLS_FWD, ACTRLS_PREV, ACTRLS_NEXT,
BCTRLS_UP, BCTRLS_DOWN, BCTRLS_LEFT, BCTRLS_RIGHT,
BCTRLS_PS1,BCTRLS_PS2,BCTRLS_PS3,BCTRLS_PS4,BCTRLS_PS5,BCTRLS_PS6,BCTRLS_PS7,BCTRLS_PS8,BCTRLS_PS9,BCTRLS_PS10,
BCTRLS_PS0,BCTRLS_PS1,BCTRLS_PS2,BCTRLS_PS3,BCTRLS_PS4,BCTRLS_PS5,BCTRLS_PS6,BCTRLS_PS7,BCTRLS_PS8,BCTRLS_PS9,
KNOB_LEFT, KNOB_RIGHT, KNOB_PUSH,
ACTRLS_REMAP, ACTRLS_MAX
} actrls_action_e;

View File

@@ -443,11 +443,11 @@ void cspotPlayer::runTask() {
CSPOT_LOG(info, "disconnecting player %s", name.c_str());
}
// we want to release memory ASAP and fore sure
// we want to release memory ASAP and for sure
centralAudioBuffer.reset();
ctx.reset();
token.clear();
// update volume when we disconnect
cJSON *config = config_alloc_get_cjson("cspot_config");
cJSON_DeleteItemFromObject(config, "volume");

View File

@@ -85,7 +85,7 @@ const static actrls_t controls = {
NULL, NULL, // rew, fwd
cspot_prev, cspot_next, // prev, next
NULL, NULL, NULL, NULL, // left, right, up, down
NULL, NULL, NULL, NULL, NULL, NULL, // pre1-6
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // pre1-10
cspot_volume_down, cspot_volume_up, cspot_toggle// knob left, knob_right, knob push
};

View File

@@ -21,7 +21,7 @@ static log_level loglevel = lINFO;
enum { BUTN_POWER_FRONT = 0X0A, BUTN_ARROW_UP, BUTN_ARROW_DOWN, BUTN_ARROW_LEFT, BUTN_KNOB_PUSH, BUTN_SEARCH,
BUTN_REW, BUTN_FWD, BUTN_PLAY, BUTN_ADD, BUTN_BRIGHTNESS, BUTN_NOW_PLAYING,
BUTN_PAUSE = 0X17, BUTN_BROWSE, BUTN_VOLUP_FRONT, BUTN_VOLDOWN_FRONT, BUTN_SIZE, BUTN_VISUAL, BUTN_VOLUMEMODE,
BUTN_PRESET_1 = 0X23, BUTN_PRESET_2, BUTN_PRESET_3, BUTN_PRESET_4, BUTN_PRESET_5, BUTN_PRESET_6, BUTN_PRESET_7, BUTN_PRESET_8, BUTN_PRESET_9, BUTN_PRESET_10,
BUTN_PRESET_0 = 0x22, BUTN_PRESET_1, BUTN_PRESET_2, BUTN_PRESET_3, BUTN_PRESET_4, BUTN_PRESET_5, BUTN_PRESET_6, BUTN_PRESET_7, BUTN_PRESET_8, BUTN_PRESET_9,
BUTN_SNOOZE,
BUTN_KNOB_LEFT = 0X5A, BUTN_KNOB_RIGHT };
@@ -143,6 +143,7 @@ LMS_CALLBACK(down, ARROW_DOWN, arrow_down)
LMS_CALLBACK(left, ARROW_LEFT, arrow_left)
LMS_CALLBACK(right, ARROW_RIGHT, arrow_right)
LMS_CALLBACK(pre0, PRESET_0, preset_0.single)
LMS_CALLBACK(pre1, PRESET_1, preset_1.single)
LMS_CALLBACK(pre2, PRESET_2, preset_2.single)
LMS_CALLBACK(pre3, PRESET_3, preset_3.single)
@@ -152,7 +153,6 @@ LMS_CALLBACK(pre6, PRESET_6, preset_6.single)
LMS_CALLBACK(pre7, PRESET_7, preset_7.single)
LMS_CALLBACK(pre8, PRESET_8, preset_8.single)
LMS_CALLBACK(pre9, PRESET_9, preset_9.single)
LMS_CALLBACK(pre10, PRESET_10, preset_10.single)
LMS_CALLBACK(knob_left, KNOB_LEFT, knob_left)
LMS_CALLBACK(knob_right, KNOB_RIGHT, knob_right)
@@ -167,7 +167,7 @@ const actrls_t LMS_controls = {
lms_prev, lms_next, // prev, next
lms_up, lms_down,
lms_left, lms_right,
lms_pre1, lms_pre2, lms_pre3, lms_pre4, lms_pre5, lms_pre6, lms_pre7, lms_pre8, lms_pre9, lms_pre10,
lms_pre0, lms_pre1, lms_pre2, lms_pre3, lms_pre4, lms_pre5, lms_pre6, lms_pre7, lms_pre8, lms_pre9,
lms_knob_left, lms_knob_right, lms_knob_push,
};

View File

@@ -247,13 +247,17 @@ void decode_close(void) {
#endif
}
void decode_flush(void) {
void decode_flush(bool close) {
LOG_INFO("decode flush");
LOCK_D;
decode.state = DECODE_STOPPED;
IF_PROCESS(
process_flush();
);
if (close && codec) {
codec->close();
codec = NULL;
}
UNLOCK_D;
}

View File

@@ -44,17 +44,17 @@
#define MAX_OPUS_FRAMES 5760
struct opus {
enum {OGG_SYNC, OGG_HEADER, OGG_PCM, OGG_DECODE} status;
enum {OGG_SYNC, OGG_ID_HEADER, OGG_COMMENT_HEADER} status;
ogg_stream_state state;
ogg_packet packet;
ogg_sync_state sync;
ogg_page page;
OpusDecoder* decoder;
int rate, gain, pre_skip;
bool fetch;
size_t overframes;
u8_t *overbuf;
int channels;
bool eos;
};
#if !LINKALL
@@ -102,15 +102,11 @@ extern struct processstate process;
#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
@@ -163,14 +159,14 @@ static int get_opus_packet(void) {
static int read_opus_header(void) {
int status = 0;
bool fetch = true;
LOCK_S;
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
while (bytes && !status) {
// first fetch a page if we need one
if (u->fetch) {
if (fetch) {
size_t consumed = min(bytes, 4096);
char* buffer = OG(&gu, sync_buffer, &u->sync, consumed);
memcpy(buffer, streambuf->readp, consumed);
@@ -180,24 +176,23 @@ static int read_opus_header(void) {
bytes -= consumed;
if (!OG(&gu, sync_pageseek, &u->sync, &u->page)) continue;
u->fetch = false;
}
//bytes = min(bytes, size);
switch (u->status) {
case OGG_SYNC:
u->status = OGG_HEADER;
u->status = OGG_ID_HEADER;
OG(&gu, stream_reset_serialno, &u->state, OG(&gu, page_serialno, &u->page));
fetch = false;
break;
case OGG_HEADER:
case OGG_ID_HEADER:
status = OG(&gu, stream_pagein, &u->state, &u->page);
if (OG(&gu, stream_packetout, &u->state, &u->packet)) {
u->status = OGG_PCM;
if (u->packet.bytes < 19 || memcmp(u->packet.packet, "OpusHead", 8)) {
LOG_ERROR("wrong opus header packet (size:%u)", u->packet.bytes);
status = -100;
break;
}
u->status = OGG_COMMENT_HEADER;
u->channels = u->packet.packet[9];
u->pre_skip = parse_uint16(u->packet.packet + 10);
u->rate = parse_uint32(u->packet.packet + 12);
@@ -207,11 +202,10 @@ static int read_opus_header(void) {
LOG_ERROR("can't create decoder %d (channels:%u)", status, u->channels);
}
}
u->fetch = true;
fetch = true;
break;
case OGG_PCM:
// loop until we have consumed VorbisComment and get ready for a new packet
u->fetch = true;
case OGG_COMMENT_HEADER:
// skip pakets to consume VorbisComment. With opus, header packets align on pages
status = OG(&gu, page_packets, &u->page);
break;
default:
@@ -226,7 +220,6 @@ static int read_opus_header(void) {
static decode_state opus_decompress(void) {
frames_t frames;
int n;
static int channels;
u8_t *write_buf;
if (decode.new_stream) {
@@ -247,8 +240,11 @@ static decode_state opus_decompress(void) {
decode.new_stream = false;
UNLOCK_O;
channels = u->channels;
if (u->channels > 2) {
LOG_WARN("too many channels: %d", u->channels);
return DECODE_ERROR;
}
LOG_INFO("setting track_start");
}
@@ -271,7 +267,7 @@ static decode_state opus_decompress(void) {
u->overframes = 0;
} else if (get_opus_packet() > 0) {
if (frames < MAX_OPUS_FRAMES) {
// don't have enough contiguous space, use the overflow buffer (still works if n < 0)
// don't have enough contiguous space, use the overflow buffer
n = OP(&gu, decode, u->decoder, u->packet.packet, u->packet.bytes, (opus_int16*) u->overbuf, MAX_OPUS_FRAMES, 0);
if (n > 0) {
u->overframes = n - min(n, frames);
@@ -287,7 +283,7 @@ static decode_state opus_decompress(void) {
} else if (!OG(&go, page_eos, &u->page)) {
UNLOCK_O_direct;
return DECODE_RUNNING;
}
} else u->eos = true;
if (n > 0) {
frames_t count;
@@ -295,7 +291,7 @@ static decode_state opus_decompress(void) {
ISAMPLE_T *optr;
frames = n;
count = frames * channels;
count = frames * u->channels;
// work backward to unpack samples (if needed)
iptr = (s16_t *) write_buf + count;
@@ -306,13 +302,13 @@ static decode_state opus_decompress(void) {
optr = (ISAMPLE_T *) write_buf + frames * 2;
)
if (channels == 2) {
if (u->channels == 2) {
#if BYTES_PER_FRAME == 8
while (count--) {
*--optr = ALIGN(*--iptr);
}
#endif
} else if (channels == 1) {
} else if (u->channels == 1) {
while (count--) {
*--optr = ALIGN(*--iptr);
*--optr = ALIGN(*iptr);
@@ -330,7 +326,7 @@ static decode_state opus_decompress(void) {
} else if (n == 0) {
if (stream.state <= DISCONNECT) {
if (stream.state <= DISCONNECT && u->eos) {
LOG_INFO("end of decode");
UNLOCK_O_direct;
return DECODE_COMPLETE;
@@ -346,7 +342,6 @@ static decode_state opus_decompress(void) {
}
UNLOCK_O_direct;
return DECODE_RUNNING;
}
@@ -356,11 +351,12 @@ static void opus_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
if (!u->overbuf) u->overbuf = malloc(MAX_OPUS_FRAMES * BYTES_PER_FRAME);
u->eos = false;
u->status = OGG_SYNC;
u->fetch = true;
u->overframes = 0;
OG(&gu, sync_init, &u->sync);
OG(&gu, sync_clear, &u->sync);
OG(&gu, stream_clear, &u->state);
OG(&gu, stream_init, &u->state, -1);
}
@@ -378,11 +374,17 @@ static void opus_close(void) {
static bool load_opus(void) {
#if !LINKALL
char *err;
void *g_handle = dlopen(LIBOGG, RTLD_NOW);
void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
if (!g_handle || !u_handle) {
LOG_INFO("dlerror: %s", dlerror());
void *u.handle = dlopen(LIBOPUS, RTLD_NOW);
if (!u_handle) {
LOG_INFO("opus dlerror: %s", dlerror());
return false;
}
void *g_handle = dlopen(LIBOGG, RTLD_NOW);
if (!g_handle) {
dlclose(u_handle);
LOG_INFO("ogg dlerror: %s", dlerror());
return false;
}
@@ -399,6 +401,7 @@ static bool load_opus(void) {
g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
u_handle->opus_decoder_create = dlsym(u_handle->handle, "opus_decoder_create");
u_handle->opus_decoder_destroy = dlsym(u_handle->handle, "opus_decoder_destroy");
u_handle->opus_decode = dlsym(u_handle->handle, "opus_decode");

View File

@@ -306,7 +306,7 @@ static void process_strm(u8_t *pkt, int len) {
break;
case 'f':
case 'q':
decode_flush();
decode_flush(strm->command == 'q');
if (!output.external) output_flush();
status.frames_played = 0;
if (stream_disconnect() && strm->command == 'f') sendSTAT("STMf", 0);

View File

@@ -622,7 +622,7 @@ struct codec {
void decode_init(log_level level, const char *include_codecs, const char *exclude_codecs);
void decode_close(void);
void decode_flush(void);
void decode_flush(bool close);
unsigned decode_newstream(unsigned sample_rate, unsigned supported_rates[]);
void codec_open(u8_t format, u8_t sample_size, u8_t sample_rate, u8_t channels, u8_t endianness);

View File

@@ -21,52 +21,81 @@
#include "squeezelite.h"
/*
* with some low-end CPU, the decode call takes a fair bit of time and if the outputbuf is locked during that
* period, the output_thread (or equivalent) will be locked although there is plenty of samples available.
* Normally, with PRIO_INHERIT, that thread should increase decoder priority and get the lock quickly but it
* seems that when the streambuf has plenty of data, the decode thread grabs the CPU to much, even it the output
* thread has a higher priority. Using an interim buffer where vorbis decoder writes the output is not great from
* an efficiency (one extra memory copy) point of view, but it allows the lock to not be kept for too long
*/
#if EMBEDDED
#define FRAME_BUF 2048
#endif
#if BYTES_PER_FRAME == 4
#define ALIGN(n) (n)
#else
#define ALIGN(n) (n << 16)
#endif
// automatically select between floating point (preferred) and fixed point libraries:
// NOTE: works with Tremor version here: http://svn.xiph.org/trunk/Tremor, not vorbisidec.1.0.2 currently in ubuntu
// we take common definations from <vorbis/vorbisfile.h> even though we can use tremor at run time
// tremor's OggVorbis_File struct is normally smaller so this is ok, but padding added to malloc in case it is bigger
#define OV_EXCLUDE_STATIC_CALLBACKS
#include <ogg/ogg.h>
#ifdef TREMOR_ONLY
#include <ivorbisfile.h>
#include <vorbis/ivorbiscodec.h>
#else
#include <vorbis/vorbisfile.h>
#include <vorbis/codec.h>
static bool tremor = false;
#endif
// this is tremor packing, not mine...
static inline int32_t clip15(int32_t x) {
int ret = x;
ret -= ((x<=32767)-1)&(x-32767);
ret -= ((x>=-32768)-1)&(x+32768);
return ret;
}
#if BYTES_PER_FRAME == 4
#define ALIGN(n) clip15((n) >> 9);
#define ALIGN_FLOAT(n) ((n)*32768.0f + 0.5f)
#else
#define ALIGN(n) (clip15((n) >> 9) << 16)
#define ALIGN_FLOAT ((n)*32768.0f*65536.0f + 0.5f)
#endif
struct vorbis {
OggVorbis_File *vf;
bool opened, end;
#if FRAME_BUF
u8_t *write_buf;
#endif
bool opened;
enum { OGG_SYNC, OGG_ID_HEADER, OGG_COMMENT_HEADER, OGG_SETUP_HEADER } status;
struct {
ogg_stream_state state;
ogg_packet packet;
ogg_sync_state sync;
ogg_page page;
};
struct {
vorbis_dsp_state decoder;
vorbis_info info;
vorbis_comment comment;
vorbis_block block;
};
int rate, channels;
uint32_t overflow;
bool eos;
};
#if !LINKALL
static struct vorbis {
// vorbis symbols to be dynamically loaded - from either vorbisfile or vorbisidec (tremor) version of library
vorbis_info *(* ov_info)(OggVorbis_File *vf, int link);
int (* ov_clear)(OggVorbis_File *vf);
long (* ov_read)(OggVorbis_File *vf, char *buffer, int length, int bigendianp, int word, int sgned, int *bitstream);
long (* ov_read_tremor)(OggVorbis_File *vf, char *buffer, int length, int *bitstream);
int (* ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
} gv;
static struct {
void *handle;
int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
int (*ogg_stream_clear)(ogg_stream_state* os);
int (*ogg_stream_reset)(ogg_stream_state* os);
int (*ogg_stream_eos)(ogg_stream_state* os);
int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
int (*ogg_sync_clear)(ogg_sync_state* oy);
void (*ogg_packet_clear)(ogg_packet* op);
char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
long (*ogg_sync_pageseek)(ogg_sync_state* oy, ogg_page* og);
int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
int (*ogg_page_packets)(const ogg_page* og);
} go;
#endif
};
static struct vorbis *v;
@@ -86,183 +115,249 @@ extern struct processstate process;
#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 OV(h, fn, ...) (ov_ ## fn)(__VA_ARGS__)
#define TREMOR(h) 0
#if !WIN
extern int ov_read_tremor(); // needed to enable compilation, not linked
#endif
#define OV(h, fn, ...) (vorbis_ ## fn)(__VA_ARGS__)
#define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
#else
#define OV(h, fn, ...) (h)->ov_##fn(__VA_ARGS__)
#define TREMOR(h) (h)->ov_read_tremor
#define OG(h, fn, ...) (h)->ogg_ ## fn(__VA_ARGS__)
#endif
// called with mutex locked within vorbis_decode to avoid locking O before S
static size_t _read_cb(void *ptr, size_t size, size_t nmemb, void *datasource) {
size_t bytes;
static int get_ogg_packet(void) {
int status = 0;
LOCK_S;
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
bytes = min(bytes, size * nmemb);
while (!(status = OG(&go, stream_packetout, &v->state, &v->packet)) && bytes) {
do {
size_t consumed = min(bytes, 4096);
char* buffer = OG(&gv, sync_buffer, &v->sync, consumed);
memcpy(buffer, streambuf->readp, consumed);
OG(&gv, sync_wrote, &v->sync, consumed);
memcpy(ptr, streambuf->readp, bytes);
_buf_inc_readp(streambuf, bytes);
_buf_inc_readp(streambuf, consumed);
bytes -= consumed;
} while (!(status = OG(&go, sync_pageseek, &v->sync, &v->page)) && bytes);
// if we have a new page, put it in
if (status) OG(&go, stream_pagein, &v->state, &v->page);
}
UNLOCK_S;
return bytes / size;
return status;
}
// these are needed for older versions of tremor, later versions and libvorbis allow NULL to be used
static int _seek_cb(void *datasource, ogg_int64_t offset, int whence) { return -1; }
static int _close_cb(void *datasource) { return 0; }
static long _tell_cb(void *datasource) { return 0; }
static decode_state vorbis_decode(void) {
static int channels;
frames_t frames;
int bytes, s, n;
u8_t *write_buf;
static int read_vorbis_header(void) {
int status = 0;
bool fetch = true;
LOCK_S;
if (stream.state <= DISCONNECT && v->end) {
UNLOCK_S;
return DECODE_COMPLETE;
size_t bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
while (bytes && !status) {
// first fetch a page if we need one
if (fetch) {
size_t consumed = min(bytes, 4096);
char* buffer = OG(&go, sync_buffer, &v->sync, consumed);
memcpy(buffer, streambuf->readp, consumed);
OG(&go, sync_wrote, &v->sync, consumed);
_buf_inc_readp(streambuf, consumed);
bytes -= consumed;
if (!OG(&go, sync_pageseek, &v->sync, &v->page)) continue;
}
switch (v->status) {
case OGG_SYNC:
v->status = OGG_ID_HEADER;
OG(&go, stream_reset_serialno, &v->state, OG(&go, page_serialno, &v->page));
fetch = false;
break;
case OGG_ID_HEADER:
status = OG(&go, stream_pagein, &v->state, &v->page);
if (!OG(&go, stream_packetout, &v->state, &v->packet)) break;
OV(&gv, info_init, &v->info);
status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet);
if (status) {
LOG_ERROR("vorbis id header packet error %d", status);
status = -1;
} else {
v->channels = v->info.channels;
v->rate = v->info.rate;
v->status = OGG_COMMENT_HEADER;
// only fetch if no other packet already in (they should not)
fetch = OG(&go, page_packets, &v->page) <= 1;
if (!fetch) LOG_INFO("id packet should terminate page");
LOG_INFO("id acquired");
}
break;
case OGG_SETUP_HEADER:
// header packets don't align with pages on Vorbis (contrary to Opus)
if (fetch) OG(&go, stream_pagein, &v->state, &v->page);
// finally build a codec if we have the packet
status = OG(&go, stream_packetout, &v->state, &v->packet);
if (status && ((status = OV(&gv, synthesis_headerin, &v->info, &v->comment, &v->packet)) ||
(status = OV(&gv, synthesis_init, &v->decoder, &v->info)))) {
LOG_ERROR("vorbis setup header packet error %d", status);
// no need to free comment, it's fake
OV(&gv, info_clear, &v->info);
status = -1;
} else {
OV(&gv, block_init, &v->decoder, &v->block);
v->opened = true;
LOG_INFO("codec up and running (rate: %d, channels:%d)", v->rate, v->channels);
status = 1;
}
//@FIXME: can we have audio on that page as well?
break;
case OGG_COMMENT_HEADER: {
// don't consume VorbisComment, just skip it
int packets = OG(&go, page_packets, &v->page);
if (packets) {
v->status = OGG_SETUP_HEADER;
OG(&go, stream_pagein, &v->state, &v->page);
OG(&go, stream_packetout, &v->state, &v->packet);
OV(&gv, comment_init, &v->comment);
v->comment.vendor = "N/A";
// because of lack of page alignment, we might have the setup page already fully in
if (packets > 1) fetch = false;
LOG_INFO("comment skipped succesfully");
}
break;
}
default:
break;
}
}
UNLOCK_S;
return status;
}
inline int pcm_out(vorbis_dsp_state* decoder, void*** pcm) {
#ifndef TREMOR_ONLY
if (!tremor) return OV(&gv, synthesis_pcmout, decoder, (ogg_float_t***) pcm);
#endif
return OV(&gv, synthesis_pcmout, decoder, (ogg_int32_t***) pcm);
}
static decode_state vorbis_decode(void) {
frames_t frames;
int n = 0;
u8_t *write_buf;
void** pcm = NULL;
if (decode.new_stream) {
ov_callbacks cbs;
int err;
struct vorbis_info *info;
int status = read_vorbis_header();
cbs.read_func = _read_cb;
if (TREMOR(v)) {
cbs.seek_func = _seek_cb; cbs.close_func = _close_cb; cbs.tell_func = _tell_cb;
} else {
cbs.seek_func = NULL; cbs.close_func = NULL; cbs.tell_func = NULL;
if (status == 0) {
return DECODE_RUNNING;
} else if (status < 0) {
LOG_WARN("can't create codec");
return DECODE_ERROR;
}
if ((err = OV(v, open_callbacks, streambuf, v->vf, NULL, 0, cbs)) < 0) {
LOG_WARN("open_callbacks error: %d", err);
return DECODE_COMPLETE;
}
v->opened = true;
info = OV(v, info, v->vf, -1);
LOG_INFO("setting track_start");
LOCK_O;
output.next_sample_rate = decode_newstream(info->rate, output.supported_rates);
output.next_sample_rate = decode_newstream(v->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;
channels = info->channels;
if (channels > 2) {
LOG_WARN("too many channels: %d", channels);
if (v->channels > 2) {
LOG_WARN("too many channels: %d", v->channels);
return DECODE_ERROR;
}
LOG_INFO("setting track_start");
}
#if FRAME_BUF
IF_DIRECT(
frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
frames = min(frames, FRAME_BUF);
write_buf = v->write_buf;
);
#else
LOCK_O_direct;
IF_DIRECT(
frames = min(_buf_space(outputbuf), _buf_cont_write(outputbuf)) / BYTES_PER_FRAME;
write_buf = outputbuf->writep;
);
#endif
IF_PROCESS(
frames = process.max_in_frames;
write_buf = process.inbuf;
);
bytes = frames * 2 * channels; // samples returned are 16 bits
v->end = frames == 0;
// write the decoded frames into outputbuf even though they are 16 bits per sample, then unpack them
#ifdef TREMOR_ONLY
n = OV(v, read, v->vf, (char *)write_buf, bytes, &s);
#else
if (!TREMOR(v)) {
#if SL_LITTLE_ENDIAN
n = OV(v, read, v->vf, (char *)write_buf, bytes, 0, 2, 1, &s);
#else
n = OV(v, read, v->vf, (char *)write_buf, bytes, 1, 2, 1, &s);
#endif
#if !WIN
} else {
n = OV(v, read_tremor, v->vf, (char *)write_buf, bytes, &s);
#endif
}
#endif
#if FRAME_BUF
LOCK_O_direct;
#endif
if (v->overflow) {
n = pcm_out(&v->decoder, &pcm);
v->overflow = n - min(n, frames);
} else if (get_ogg_packet() > 0) {
n = OV(&gv, synthesis, &v->block, &v->packet);
if (n == 0) n = OV(&gv, synthesis_blockin, &v->decoder, &v->block);
if (n == 0) n = pcm_out(&v->decoder, &pcm);
v->overflow = n - min(n, frames);
} else if (!OG(&go, page_eos, &v->page)) {
UNLOCK_O_direct;
return DECODE_RUNNING;
} else v->eos = true;
if (n > 0) {
frames_t count;
s16_t *iptr;
ISAMPLE_T *optr;
ISAMPLE_T *optr = (ISAMPLE_T*) write_buf;
frames = min(n, frames);
frames_t count = frames;
#ifndef TREMOR_ONLY
if (!tremor) {
if (v->channels == 2) {
float* iptr_l = (float*) pcm[0];
float* iptr_r = (float*) pcm[1];
frames = n / 2 / channels;
count = frames * channels;
// work backward to unpack samples (if needed)
iptr = (s16_t *) write_buf + count;
IF_DIRECT(
optr = (ISAMPLE_T *) outputbuf->writep + frames * 2;
)
IF_PROCESS(
optr = (ISAMPLE_T *) write_buf + frames * 2;
)
if (channels == 2) {
#if BYTES_PER_FRAME == 4
#if FRAME_BUF
// copy needed only when DIRECT and FRAME_BUF
IF_DIRECT(
memcpy(outputbuf->writep, write_buf, frames * BYTES_PER_FRAME);
)
#endif
while (count--) {
*optr++ = ALIGN_FLOAT(*iptr_l++);
*optr++ = ALIGN_FLOAT(*iptr_r++);;
}
} else if (v->channels == 1) {
float* iptr = pcm[0];
while (count--) {
*optr++ = ALIGN_FLOAT(*iptr);
*optr++ = ALIGN_FLOAT(*iptr++);
}
}
} else
#else
while (count--) {
*--optr = ALIGN(*--iptr);
}
{
if (v->channels == 2) {
s32_t* iptr_l = (s32_t*) pcm[0];
s32_t* iptr_r = (s32_t*) pcm[1];
while (count--) {
*optr++ = ALIGN(*iptr_l++);
*optr++ = ALIGN(*iptr_r++);
}
} else if (v->channels == 1) {
s32_t* iptr = (s32_t*) pcm[0];
while (count--) {
*optr++ = ALIGN(*iptr);
*optr++ = ALIGN(*iptr++);
}
}
}
#endif
} else if (channels == 1) {
while (count--) {
*--optr = ALIGN(*--iptr);
*--optr = ALIGN(*iptr);
}
}
// return samples to vorbis/tremor decoder
OV(&gv, synthesis_read, &v->decoder, frames);
IF_DIRECT(
_buf_inc_writep(outputbuf, frames * BYTES_PER_FRAME);
@@ -275,19 +370,14 @@ static decode_state vorbis_decode(void) {
} else if (n == 0) {
if (stream.state <= DISCONNECT) {
LOG_INFO("partial decode");
if (stream.state <= DISCONNECT && v->eos) {
LOG_INFO("end of decode");
UNLOCK_O_direct;
return DECODE_COMPLETE;
} else {
LOG_INFO("no frame decoded");
}
} else if (n == OV_HOLE) {
// recoverable hole in stream, seen when skipping
LOG_DEBUG("hole in stream");
} else {
LOG_INFO("ov_read error: %d", n);
@@ -300,55 +390,81 @@ static decode_state vorbis_decode(void) {
}
static void vorbis_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
if (!v->vf) {
v->vf = malloc(sizeof(OggVorbis_File) + 128); // add some padding as struct size may be larger
memset(v->vf, 0, sizeof(OggVorbis_File) + 128);
#if FRAME_BUF
v->write_buf = malloc(FRAME_BUF * BYTES_PER_FRAME);
#endif
} else {
if (v->opened) {
OV(v, clear, v->vf);
v->opened = false;
}
LOG_INFO("OPENING CODEC");
if (v->opened) {
OV(&go, block_clear, &v->block);
OV(&go, info_clear, &v->info);
OV(&go, dsp_clear, &v->decoder);
}
v->eos = false;
v->opened = false;
v->status = OGG_SYNC;
v->overflow = 0;
OG(&gu, sync_clear, &v->sync);
OG(&gu, stream_clear, &v->state);
OG(&gu, stream_init, &v->state, -1);
}
static void vorbis_close(void) {
static void vorbis_close() {
return;
LOG_INFO("CLOSING CODEC");
if (v->opened) {
OV(v, clear, v->vf);
v->opened = false;
OV(&go, block_clear, &v->block);
OV(&go, info_clear, &v->info);
OV(&go, dsp_clear, &v->decoder);
}
free(v->vf);
#if FRAME_BUF
free(v->write_buf);
v->write_buf = NULL;
#endif
v->vf = NULL;
v->end = false;
v->opened = false;
OG(&go, stream_clear, &v->state);
OG(&go, sync_clear, &v->sync);
}
static bool load_vorbis() {
#if !LINKALL
void *handle = dlopen(LIBVORBIS, RTLD_NOW);
char *err;
bool tremor = false;
if (!handle) {
handle = dlopen(LIBTREMOR, RTLD_NOW);
if (handle) {
char *err;
void *g_handle = dlopen(LIBOGG, RTLD_NOW);
if (!g_handle) {
LOG_INFO("ogg dlerror: %s", dlerror());
return false
}
void *v_handle = NULL;
#ifndef TREMOR_ONLY
v_handle = dlopen(LIBVORBIS, RTLD_NOW);
#endif
if (!v_handle) {
v_handle = dlopen(LIBTREMOR, RTLD_NOW);
if (v_handle) {
tremor = true;
} else {
LOG_INFO("dlerror: %s", dlerror());
dlclose(g_handle);
LOG_INFO("vorbis/tremor dlerror: %s", dlerror());
return false;
}
}
g_handle->ogg_stream_clear = dlsym(g_handle->handle, "ogg_stream_clear");
g_handle->ogg_stream_reset = dlsym(g_handle->handle, "ogg_stream_reset");
g_handle->ogg_stream_eos = dlsym(g_handle->handle, "ogg_stream_eos");
g_handle->ogg_stream_reset_serialno = dlsym(g_handle->handle, "ogg_stream_reset_serialno");
g_handle->ogg_sync_clear = dlsym(g_handle->handle, "ogg_sync_clear");
g_handle->ogg_packet_clear = dlsym(g_handle->handle, "ogg_packet_clear");
g_handle->ogg_sync_buffer = dlsym(g_handle->handle, "ogg_sync_buffer");
g_handle->ogg_sync_wrote = dlsym(g_handle->handle, "ogg_sync_wrote");
g_handle->ogg_sync_pageseek = dlsym(g_handle->handle, "ogg_sync_pageseek");
g_handle->ogg_sync_pageout = dlsym(g_handle->handle, "ogg_sync_pageout");
g_handle->ogg_stream_pagein = dlsym(g_handle->handle, "ogg_stream_pagein");
g_handle->ogg_stream_packetout = dlsym(g_handle->handle, "ogg_stream_packetout");
g_handle->ogg_page_packets = dlsym(g_handle->handle, "ogg_page_packets");
v->ov_read = tremor ? NULL : dlsym(handle, "ov_read");
v->ov_read_tremor = tremor ? dlsym(handle, "ov_read") : NULL;
v->ov_info = dlsym(handle, "ov_info");
v->ov_clear = dlsym(handle, "ov_clear");
v->ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
v_handle.ov_read = dlsym(handle, "ov_read");
v_handle.ov_info = dlsym(handle, "ov_info");
v_handle.ov_clear = dlsym(handle, "ov_clear");
v_handle.ov_open_callbacks = dlsym(handle, "ov_open_callbacks");
if ((err = dlerror()) != NULL) {
LOG_INFO("dlerror: %s", err);
@@ -372,12 +488,10 @@ struct codec *register_vorbis(void) {
vorbis_decode,// decode
};
v = malloc(sizeof(struct vorbis));
if (!v) {
if ((v = calloc(1, sizeof(struct vorbis))) == NULL) {
return NULL;
}
v->vf = NULL;
v->opened = false;
if (!load_vorbis()) {