#include <math.h>
#define BITSTREAM_READER_LE
+#include "libavutil/float_dsp.h"
#include "avcodec.h"
#include "get_bits.h"
#include "dsputil.h"
#include "fft.h"
#include "fmtconvert.h"
+#include "internal.h"
#include "vorbis.h"
#include "xiph.h"
AVFrame frame;
GetBitContext gb;
DSPContext dsp;
+ AVFloatDSPContext fdsp;
FmtConvertContext fmt_conv;
FFTContext mdct[2];
uint8_t mode_number; // mode number for the current packet
uint8_t previous_window;
float *channel_residues;
- float *channel_floors;
float *saved;
- float scale_bias; // for float->int conversion
} vorbis_context;
/* Helper functions */
int i;
av_freep(&vc->channel_residues);
- av_freep(&vc->channel_floors);
av_freep(&vc->saved);
for (i = 0; i < vc->residue_count; i++)
for (i = 0; i < vc->codebook_count; ++i) {
av_free(vc->codebooks[i].codevectors);
- free_vlc(&vc->codebooks[i].vlc);
+ ff_free_vlc(&vc->codebooks[i].vlc);
}
av_freep(&vc->codebooks);
}
// Precalculate order of x coordinates - needed for decode
- ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
+ if (ff_vorbis_ready_floor1_list(vc->avccontext,
+ floor_setup->data.t1.list,
+ floor_setup->data.t1.x_list_dim)) {
+ return AVERROR_INVALIDDATA;
+ }
} else if (floor_setup->floor_type == 0) {
unsigned max_codebook_dim = 0;
res_setup->partition_size = get_bits(gb, 24) + 1;
/* Validations to prevent a buffer overflow later. */
if (res_setup->begin>res_setup->end ||
- res_setup->end > vc->avccontext->channels * vc->blocksize[1] / 2 ||
+ res_setup->end > (res_setup->type == 2 ? vc->avccontext->channels : 1) * vc->blocksize[1] / 2 ||
(res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
av_log(vc->avccontext, AV_LOG_ERROR,
"partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
}
vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
- vc->channel_floors = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_floors));
vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
vc->previous_window = 0;
- ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
- ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
+ ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
+ ff_mdct_init(&vc->mdct[1], bl1, 1, -1.0);
av_dlog(NULL, " vorbis version %d \n audio_channels %d \n audio_samplerate %d \n bitrate_max %d \n bitrate_nom %d \n bitrate_min %d \n blk_0 %d blk_1 %d \n ",
vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
int hdr_type, ret;
vc->avccontext = avccontext;
- dsputil_init(&vc->dsp, avccontext);
+ ff_dsputil_init(&vc->dsp, avccontext);
+ avpriv_float_dsp_init(&vc->fdsp, avccontext->flags & CODEC_FLAG_BITEXACT);
ff_fmt_convert_init(&vc->fmt_conv, avccontext);
- if (avccontext->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
- avccontext->sample_fmt = AV_SAMPLE_FMT_FLT;
- vc->scale_bias = 1.0f;
- } else {
- avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
- vc->scale_bias = 32768.0f;
- }
+ avccontext->sample_fmt = AV_SAMPLE_FMT_FLTP;
if (!headers_len) {
av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
avccontext->channels = vc->audio_channels;
avccontext->sample_rate = vc->audio_samplerate;
- avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
avcodec_get_frame_defaults(&vc->frame);
avccontext->coded_frame = &vc->frame;
floor1_flag[i] = 1;
if (val >= room) {
if (highroom > lowroom) {
- floor1_Y_final[i] = val - lowroom + predicted;
+ floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
} else {
- floor1_Y_final[i] = predicted - val + highroom - 1;
+ floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
}
} else {
if (val & 1) {
- floor1_Y_final[i] = predicted - (val + 1) / 2;
+ floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
} else {
- floor1_Y_final[i] = predicted + val / 2;
+ floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
}
}
} else {
floor1_flag[i] = 0;
- floor1_Y_final[i] = predicted;
+ floor1_Y_final[i] = av_clip_uint16(predicted);
}
av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
uint8_t *do_not_decode,
float *vec,
unsigned vlen,
+ unsigned ch_left,
int vr_type)
{
GetBitContext *gb = &vc->gb;
unsigned ptns_to_read = vr->ptns_to_read;
uint8_t *classifs = vr->classifs;
unsigned pass, ch_used, i, j, k, l;
+ unsigned max_output = (ch - 1) * vlen;
if (vr_type == 2) {
for (j = 1; j < ch; ++j)
if (do_not_decode[0])
return 0;
ch_used = 1;
+ max_output += vr->end / ch;
} else {
ch_used = ch;
+ max_output += vr->end;
+ }
+
+ if (max_output > ch_left * vlen) {
+ av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
+ return -1;
}
av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
unsigned coffs;
unsigned dim = vc->codebooks[vqbook].dimensions;
- unsigned step = dim == 1 ? vr->partition_size
- : FASTDIV(vr->partition_size, dim);
+ unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
vorbis_codebook codebook = vc->codebooks[vqbook];
if (vr_type == 0) {
for (k = 0; k < step; ++k) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
for (l = 0; l < dim; ++l)
- vec[voffs + k + l * step] += codebook.codevectors[coffs + l]; // FPMATH
+ vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
}
} else if (vr_type == 1) {
voffs = voffset + j * vlen;
for (k = 0; k < step; ++k) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
for (l = 0; l < dim; ++l, ++voffs) {
- vec[voffs]+=codebook.codevectors[coffs+l]; // FPMATH
+ vec[voffs]+=codebook.codevectors[coffs+l];
av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
if (dim == 2) {
for (k = 0; k < step; ++k) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
- vec[voffs + k ] += codebook.codevectors[coffs ]; // FPMATH
- vec[voffs + k + vlen] += codebook.codevectors[coffs + 1]; // FPMATH
+ vec[voffs + k ] += codebook.codevectors[coffs ];
+ vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
}
} else if (dim == 4) {
for (k = 0; k < step; ++k, voffs += 2) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
- vec[voffs ] += codebook.codevectors[coffs ]; // FPMATH
- vec[voffs + 1 ] += codebook.codevectors[coffs + 2]; // FPMATH
- vec[voffs + vlen ] += codebook.codevectors[coffs + 1]; // FPMATH
- vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3]; // FPMATH
+ vec[voffs ] += codebook.codevectors[coffs ];
+ vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
+ vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
+ vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
}
} else
for (k = 0; k < step; ++k) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
for (l = 0; l < dim; l += 2, voffs++) {
- vec[voffs ] += codebook.codevectors[coffs + l ]; // FPMATH
- vec[voffs + vlen] += codebook.codevectors[coffs + l + 1]; // FPMATH
+ vec[voffs ] += codebook.codevectors[coffs + l ];
+ vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
pass, voffset / ch + (voffs % ch) * vlen,
}
} else if (vr_type == 2) {
- voffs = voffset;
+ unsigned voffs_div = FASTDIV(voffset << 1, ch <<1);
+ unsigned voffs_mod = voffset - voffs_div * ch;
for (k = 0; k < step; ++k) {
coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
- for (l = 0; l < dim; ++l, ++voffs) {
- vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l]; // FPMATH FIXME use if and counter instead of / and %
+ for (l = 0; l < dim; ++l) {
+ vec[voffs_div + voffs_mod * vlen] +=
+ codebook.codevectors[coffs + l];
av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
- pass, voffset / ch + (voffs % ch) * vlen,
- vec[voffset / ch + (voffs % ch) * vlen],
+ pass, voffs_div + voffs_mod * vlen,
+ vec[voffs_div + voffs_mod * vlen],
codebook.codevectors[coffs + l], coffs, l);
+
+ if (++voffs_mod == ch) {
+ voffs_div++;
+ voffs_mod = 0;
+ }
}
}
}
static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
unsigned ch,
uint8_t *do_not_decode,
- float *vec, unsigned vlen)
+ float *vec, unsigned vlen,
+ unsigned ch_left)
{
if (vr->type == 2)
- return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 2);
+ return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
else if (vr->type == 1)
- return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 1);
+ return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
else if (vr->type == 0)
- return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 0);
+ return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
else {
av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
return AVERROR_INVALIDDATA;
}
}
-void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
+void ff_vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
{
int i;
for (i = 0; i < blocksize; i++) {
// Decode the audio packet using the functions above
-static int vorbis_parse_audio_packet(vorbis_context *vc)
+static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
{
GetBitContext *gb = &vc->gb;
FFTContext *mdct;
uint8_t do_not_decode[255];
vorbis_mapping *mapping;
float *ch_res_ptr = vc->channel_residues;
- float *ch_floor_ptr = vc->channel_floors;
uint8_t res_chan[255];
unsigned res_num = 0;
int retlen = 0;
+ unsigned ch_left = vc->audio_channels;
+ unsigned vlen;
if (get_bits1(gb)) {
av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
blockflag = vc->modes[mode_number].blockflag;
blocksize = vc->blocksize[blockflag];
- if (blockflag)
- skip_bits(gb, 2); // previous_window, next_window
+ vlen = blocksize / 2;
+ if (blockflag) {
+ previous_window = get_bits(gb, 1);
+ skip_bits1(gb); // next_window
+ }
- memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
- memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2); //FIXME can this be removed ?
+ memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
+ for (i = 0; i < vc->audio_channels; ++i)
+ memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
// Decode floor
floor = &vc->floors[mapping->submap_floor[0]];
}
- ret = floor->decode(vc, &floor->data, ch_floor_ptr);
+ ret = floor->decode(vc, &floor->data, floor_ptr[i]);
if (ret < 0) {
av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
return AVERROR_INVALIDDATA;
}
no_residue[i] = ret;
- ch_floor_ptr += blocksize / 2;
}
// Nonzero vector propagate
for (i = 0; i < mapping->submaps; ++i) {
vorbis_residue *residue;
unsigned ch = 0;
+ int ret;
for (j = 0; j < vc->audio_channels; ++j) {
if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
}
}
residue = &vc->residues[mapping->submap_residue[i]];
- vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2);
+ if (ch_left < ch) {
+ av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
+ return -1;
+ }
+ if (ch) {
+ ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
+ if (ret < 0)
+ return ret;
+ }
- ch_res_ptr += ch * blocksize / 2;
+ ch_res_ptr += ch * vlen;
+ ch_left -= ch;
}
+ if (ch_left > 0)
+ return AVERROR_INVALIDDATA;
+
// Inverse coupling
for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
mdct = &vc->mdct[blockflag];
for (j = vc->audio_channels-1;j >= 0; j--) {
- ch_floor_ptr = vc->channel_floors + j * blocksize / 2;
ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
- vc->dsp.vector_fmul(ch_floor_ptr, ch_floor_ptr, ch_res_ptr, blocksize / 2);
- mdct->imdct_half(mdct, ch_res_ptr, ch_floor_ptr);
+ vc->fdsp.vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
+ mdct->imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
}
-// Overlap/add, save data for next overlapping FPMATH
+// Overlap/add, save data for next overlapping
retlen = (blocksize + vc->blocksize[previous_window]) / 4;
for (j = 0; j < vc->audio_channels; j++) {
unsigned bs1 = vc->blocksize[1];
float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
float *saved = vc->saved + j * bs1 / 4;
- float *ret = vc->channel_floors + j * retlen;
+ float *ret = floor_ptr[j];
float *buf = residue;
const float *win = vc->win[blockflag & previous_window];
int buf_size = avpkt->size;
vorbis_context *vc = avccontext->priv_data;
GetBitContext *gb = &vc->gb;
- const float *channel_ptrs[255];
+ float *channel_ptrs[255];
int i, len, ret;
av_dlog(NULL, "packet length %d \n", buf_size);
+ /* get output buffer */
+ vc->frame.nb_samples = vc->blocksize[1] / 2;
+ if ((ret = ff_get_buffer(avccontext, &vc->frame)) < 0) {
+ av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
+ return ret;
+ }
+
+ if (vc->audio_channels > 8) {
+ for (i = 0; i < vc->audio_channels; i++)
+ channel_ptrs[i] = (float *)vc->frame.extended_data[i];
+ } else {
+ for (i = 0; i < vc->audio_channels; i++) {
+ int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
+ channel_ptrs[ch] = (float *)vc->frame.extended_data[i];
+ }
+ }
+
init_get_bits(gb, buf, buf_size*8);
- if ((len = vorbis_parse_audio_packet(vc)) <= 0)
+ if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
return len;
if (!vc->first_frame) {
av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
- /* get output buffer */
vc->frame.nb_samples = len;
- if ((ret = avccontext->get_buffer(avccontext, &vc->frame)) < 0) {
- av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
- return ret;
- }
-
- if (vc->audio_channels > 8) {
- for (i = 0; i < vc->audio_channels; i++)
- channel_ptrs[i] = vc->channel_floors + i * len;
- } else {
- for (i = 0; i < vc->audio_channels; i++)
- channel_ptrs[i] = vc->channel_floors +
- len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
- }
-
- if (avccontext->sample_fmt == AV_SAMPLE_FMT_FLT)
- vc->fmt_conv.float_interleave((float *)vc->frame.data[0], channel_ptrs,
- len, vc->audio_channels);
- else
- vc->fmt_conv.float_to_int16_interleave((int16_t *)vc->frame.data[0],
- channel_ptrs, len,
- vc->audio_channels);
-
*got_frame_ptr = 1;
*(AVFrame *)data = vc->frame;
return 0;
}
+static av_cold void vorbis_decode_flush(AVCodecContext *avccontext)
+{
+ vorbis_context *vc = avccontext->priv_data;
+
+ if (vc->saved) {
+ memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
+ sizeof(*vc->saved));
+ }
+ vc->previous_window = 0;
+}
+
AVCodec ff_vorbis_decoder = {
- .name = "vorbis",
- .type = AVMEDIA_TYPE_AUDIO,
- .id = CODEC_ID_VORBIS,
- .priv_data_size = sizeof(vorbis_context),
- .init = vorbis_decode_init,
- .close = vorbis_decode_close,
- .decode = vorbis_decode_frame,
- .capabilities = CODEC_CAP_DR1,
- .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
+ .name = "vorbis",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_VORBIS,
+ .priv_data_size = sizeof(vorbis_context),
+ .init = vorbis_decode_init,
+ .close = vorbis_decode_close,
+ .decode = vorbis_decode_frame,
+ .flush = vorbis_decode_flush,
+ .capabilities = CODEC_CAP_DR1,
+ .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
.channel_layouts = ff_vorbis_channel_layouts,
- .sample_fmts = (const enum AVSampleFormat[]) {
- AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
- },
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+ AV_SAMPLE_FMT_NONE },
};
-