2019-03-02 11:17:50 +01:00
|
|
|
#include <math.h>
|
2017-04-29 22:37:15 +02:00
|
|
|
#include "coding.h"
|
|
|
|
#include "../util.h"
|
2008-06-15 06:01:03 +02:00
|
|
|
|
|
|
|
#ifdef VGM_USE_VORBIS
|
|
|
|
#include <vorbis/vorbisfile.h>
|
|
|
|
|
|
|
|
|
2019-03-24 02:45:03 +01:00
|
|
|
static void pcm_convert_float_to_16(int channels, sample_t * outbuf, int samples_to_do, float ** pcm, int disable_ordering);
|
2008-06-15 06:01:03 +02:00
|
|
|
|
2015-05-13 20:11:24 +02:00
|
|
|
|
2019-03-02 11:17:50 +01:00
|
|
|
void decode_ogg_vorbis(ogg_vorbis_codec_data * data, sample_t * outbuf, int32_t samples_to_do, int channels) {
|
|
|
|
int samples_done = 0;
|
|
|
|
long rc;
|
|
|
|
float **pcm_channels; /* pointer to Xiph's double array buffer */
|
|
|
|
|
|
|
|
while (samples_done < samples_to_do) {
|
|
|
|
rc = ov_read_float(
|
|
|
|
&data->ogg_vorbis_file, /* context */
|
|
|
|
&pcm_channels, /* buffer pointer */
|
|
|
|
(samples_to_do - samples_done), /* samples to produce */
|
|
|
|
&data->bitstream); /* bitstream*/
|
|
|
|
if (rc <= 0) goto fail; /* rc is samples done */
|
|
|
|
|
2019-03-24 02:45:03 +01:00
|
|
|
pcm_convert_float_to_16(channels, outbuf, rc, pcm_channels, data->disable_reordering);
|
2019-03-02 11:17:50 +01:00
|
|
|
|
|
|
|
outbuf += rc * channels;
|
|
|
|
samples_done += rc;
|
|
|
|
|
|
|
|
|
|
|
|
#if 0 // alt decoding
|
|
|
|
/* we use ov_read_float as to reuse the xiph's buffer for easier remapping,
|
|
|
|
* but seems ov_read is slightly faster due to optimized (asm) float-to-int. */
|
|
|
|
rc = ov_read(
|
|
|
|
&data->ogg_vorbis_file, /* context */
|
|
|
|
(char *)(outbuf), /* buffer */
|
|
|
|
(samples_to_do - samples_done) * sizeof(sample_t) * channels, /* length in bytes */
|
|
|
|
0, /* pcm endianness */
|
|
|
|
sizeof(sample), /* pcm size */
|
|
|
|
1, /* pcm signedness */
|
|
|
|
&data->bitstream); /* bitstream */
|
|
|
|
if (rc <= 0) goto fail; /* rc is bytes done (for all channels) */
|
|
|
|
|
|
|
|
swap_samples_le(outbuf, rc / sizeof(sample_t)); /* endianness is a bit weird with ov_read though */
|
|
|
|
|
|
|
|
outbuf += rc / sizeof(sample_t);
|
|
|
|
samples_done += rc / sizeof(sample_t) / channels;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
fail:
|
|
|
|
VGM_LOG("OGG: error %lx during decode\n", rc);
|
|
|
|
memset(outbuf, 0, (samples_to_do - samples_done) * channels * sizeof(sample));
|
2008-06-15 06:01:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 11:17:50 +01:00
|
|
|
/* vorbis encodes channels in non-standard order, so we remap during conversion to fix this oddity.
|
|
|
|
* (feels a bit weird as one would think you could leave as-is and set the player's output
|
|
|
|
* order, but that isn't possible and remapping is what FFmpeg and every other plugin do). */
|
|
|
|
static const int xiph_channel_map[8][8] = {
|
|
|
|
{ 0 }, /* 1ch: FC > same */
|
|
|
|
{ 0, 1 }, /* 2ch: FL FR > same */
|
|
|
|
{ 0, 2, 1 }, /* 3ch: FL FC FR > FL FR FC */
|
|
|
|
{ 0, 1, 2, 3 }, /* 4ch: FL FR BL BR > same */
|
|
|
|
{ 0, 2, 1, 3, 4 }, /* 5ch: FL FC FR BL BR > FL FR FC BL BR */
|
|
|
|
{ 0, 2, 1, 5, 3, 4 }, /* 6ch: FL FC FR BL BR LFE > FL FR FC LFE BL BR */
|
|
|
|
{ 0, 2, 1, 6, 5, 3, 4 }, /* 7ch: FL FC FR SL SR BC LFE > FL FR FC LFE BC SL SR */
|
|
|
|
{ 0, 2, 1, 7, 5, 6, 3, 4 }, /* 8ch: FL FC FR SL SR BL BR LFE > FL FR FC LFE BL BR SL SR */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* converts from internal Vorbis format to standard PCM and remaps (mostly from Xiph's decoder_example.c) */
|
2019-03-24 02:45:03 +01:00
|
|
|
static void pcm_convert_float_to_16(int channels, sample_t * outbuf, int samples_to_do, float ** pcm, int disable_ordering) {
|
2019-03-02 11:17:50 +01:00
|
|
|
int ch, s, ch_map;
|
|
|
|
sample_t *ptr;
|
|
|
|
float *channel;
|
|
|
|
|
|
|
|
/* convert float PCM (multichannel float array, with pcm[0]=ch0, pcm[1]=ch1, pcm[2]=ch0, etc)
|
|
|
|
* to 16 bit signed PCM ints (host order) and interleave + fix clipping */
|
|
|
|
for (ch = 0; ch < channels; ch++) {
|
2019-03-24 02:45:03 +01:00
|
|
|
ch_map = disable_ordering ?
|
|
|
|
ch :
|
|
|
|
(channels > 8) ? ch : xiph_channel_map[channels - 1][ch]; /* put Vorbis' ch to other outbuf's ch */
|
2019-03-02 11:17:50 +01:00
|
|
|
ptr = outbuf + ch;
|
|
|
|
channel = pcm[ch_map];
|
|
|
|
for (s = 0; s < samples_to_do; s++) {
|
|
|
|
int val = (int)floor(channel[s] * 32767.0f + 0.5f); /* use floorf? doesn't seem any faster */
|
|
|
|
if (val > 32767) val = 32767;
|
|
|
|
else if (val < -32768) val = -32768;
|
|
|
|
|
|
|
|
*ptr = val;
|
|
|
|
ptr += channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ********************************************** */
|
2017-04-29 22:37:15 +02:00
|
|
|
|
|
|
|
void reset_ogg_vorbis(VGMSTREAM *vgmstream) {
|
|
|
|
ogg_vorbis_codec_data *data = vgmstream->codec_data;
|
2018-03-10 16:59:00 +01:00
|
|
|
if (!data) return;
|
|
|
|
|
2019-08-15 22:15:37 +02:00
|
|
|
/* this seek cleans internal buffers */
|
2019-03-02 11:17:50 +01:00
|
|
|
ov_pcm_seek(&data->ogg_vorbis_file, 0);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void seek_ogg_vorbis(VGMSTREAM *vgmstream, int32_t num_sample) {
|
2019-03-02 11:17:50 +01:00
|
|
|
ogg_vorbis_codec_data *data = vgmstream->codec_data;
|
2018-03-10 16:59:00 +01:00
|
|
|
if (!data) return;
|
|
|
|
|
2019-08-15 22:15:37 +02:00
|
|
|
/* this seek crosslaps to avoid possible clicks, so seeking to 0 will
|
|
|
|
* decode a bit differently than ov_pcm_seek */
|
2019-03-02 11:17:50 +01:00
|
|
|
ov_pcm_seek_lap(&data->ogg_vorbis_file, num_sample);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_ogg_vorbis(ogg_vorbis_codec_data *data) {
|
2019-03-02 11:17:50 +01:00
|
|
|
if (!data) return;
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2019-03-02 11:17:50 +01:00
|
|
|
ov_clear(&data->ogg_vorbis_file);
|
2017-04-29 22:37:15 +02:00
|
|
|
|
2019-03-02 11:17:50 +01:00
|
|
|
close_streamfile(data->ov_streamfile.streamfile);
|
|
|
|
free(data);
|
2017-04-29 22:37:15 +02:00
|
|
|
}
|
|
|
|
|
2008-06-15 06:01:03 +02:00
|
|
|
#endif
|