*/
/**
- * @file libavcodec/mlpdec.c
+ * @file
* MLP decoder
*/
#include <stdint.h>
#include "avcodec.h"
+#include "dsputil.h"
#include "libavutil/intreadwrite.h"
#include "get_bits.h"
#include "libavutil/crc.h"
//! For each channel output by the matrix, the output channel to map it to
uint8_t ch_assign[MAX_CHANNELS];
+ //! Channel coding parameters for channels in the substream
+ ChannelParams channel_params[MAX_CHANNELS];
+
//! The left shift applied to random noise in 0x31ea substreams.
uint8_t noise_shift;
//! The current seed value for the pseudorandom noise generator(s).
SubStream substream[MAX_SUBSTREAMS];
- ChannelParams channel_params[MAX_CHANNELS];
-
int matrix_changed;
int filter_changed[MAX_CHANNELS][NUM_FILTERS];
int8_t noise_buffer[MAX_BLOCKSIZE_POW2];
int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS];
+
+ DSPContext dsp;
} MLPDecodeContext;
static VLC huff_vlc[3];
static av_cold void init_static(void)
{
- INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
- &ff_mlp_huffman_tables[0][0][1], 2, 1,
- &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
- INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
- &ff_mlp_huffman_tables[1][0][1], 2, 1,
- &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
- INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
- &ff_mlp_huffman_tables[2][0][1], 2, 1,
- &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
+ if (!huff_vlc[0].bits) {
+ INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
+ &ff_mlp_huffman_tables[0][0][1], 2, 1,
+ &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
+ INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
+ &ff_mlp_huffman_tables[1][0][1], 2, 1,
+ &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
+ INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
+ &ff_mlp_huffman_tables[2][0][1], 2, 1,
+ &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
+ }
ff_mlp_init_crc();
}
static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
unsigned int substr, unsigned int ch)
{
- ChannelParams *cp = &m->channel_params[ch];
SubStream *s = &m->substream[substr];
+ ChannelParams *cp = &s->channel_params[ch];
int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
int32_t sign_huff_offset = cp->huff_offset;
m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
for (channel = s->min_channel; channel <= s->max_channel; channel++) {
- ChannelParams *cp = &m->channel_params[channel];
+ ChannelParams *cp = &s->channel_params[channel];
int codebook = cp->codebook;
int quant_step_size = s->quant_step_size[channel];
int lsb_bits = cp->huff_lsbs - quant_step_size;
m->avctx = avctx;
for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
m->substream[substr].lossless_check_data = 0xffffffff;
+ dsputil_init(&m->dsp, avctx);
return 0;
}
m->avctx->bits_per_raw_sample = mh.group1_bits;
if (mh.group1_bits > 16)
- m->avctx->sample_fmt = SAMPLE_FMT_S32;
+ m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
else
- m->avctx->sample_fmt = SAMPLE_FMT_S16;
+ m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
m->params_valid = 1;
for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
: MAX_MATRIX_CHANNEL_TRUEHD;
sync_word = get_bits(gbp, 13);
- s->noise_type = get_bits1(gbp);
- if ((m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) ||
- sync_word != 0x31ea >> 1) {
+ if (sync_word != 0x31ea >> 1) {
av_log(m->avctx, AV_LOG_ERROR,
"restart header sync incorrect (got 0x%04x)\n", sync_word);
return -1;
}
+ s->noise_type = get_bits1(gbp);
+
+ if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
+ av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
+ return -1;
+ }
+
skip_bits(gbp, 16); /* Output timestamp */
s->min_channel = get_bits(gbp, 4);
return -1;
}
+ /* This should happen for TrueHD streams with >6 channels and MLP's noise
+ * type. It is not yet known if this is allowed. */
+ if (s->max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
+ av_log(m->avctx, AV_LOG_ERROR,
+ "Number of channels %d is larger than the maximum supported "
+ "by the decoder. %s\n", s->max_channel+2, sample_message);
+ return -1;
+ }
+
if (s->min_channel > s->max_channel) {
av_log(m->avctx, AV_LOG_ERROR,
"Substream min channel cannot be greater than max channel.\n");
if (m->avctx->request_channels > 0
&& s->max_channel + 1 >= m->avctx->request_channels
&& substr < m->max_decoded_substream) {
- av_log(m->avctx, AV_LOG_INFO,
+ av_log(m->avctx, AV_LOG_DEBUG,
"Extracting %d channel downmix from substream %d. "
"Further substreams will be skipped.\n",
s->max_channel + 1, substr);
memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
for (ch = s->min_channel; ch <= s->max_channel; ch++) {
- ChannelParams *cp = &m->channel_params[ch];
+ ChannelParams *cp = &s->channel_params[ch];
cp->filter_params[FIR].order = 0;
cp->filter_params[IIR].order = 0;
cp->filter_params[FIR].shift = 0;
/** Read parameters for one of the prediction filters. */
static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
- unsigned int channel, unsigned int filter)
+ unsigned int substr, unsigned int channel,
+ unsigned int filter)
{
- FilterParams *fp = &m->channel_params[channel].filter_params[filter];
+ SubStream *s = &m->substream[substr];
+ FilterParams *fp = &s->channel_params[channel].filter_params[filter];
const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
const char fchar = filter ? 'I' : 'F';
int i, order;
fp->order = order;
if (order > 0) {
+ int32_t *fcoeff = s->channel_params[channel].coeff[filter];
int coeff_bits, coeff_shift;
fp->shift = get_bits(gbp, 4);
}
for (i = 0; i < order; i++)
- fp->coeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
+ fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
if (get_bits1(gbp)) {
int state_bits, state_shift;
static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
GetBitContext *gbp, unsigned int ch)
{
- ChannelParams *cp = &m->channel_params[ch];
+ SubStream *s = &m->substream[substr];
+ ChannelParams *cp = &s->channel_params[ch];
FilterParams *fir = &cp->filter_params[FIR];
FilterParams *iir = &cp->filter_params[IIR];
- SubStream *s = &m->substream[substr];
if (s->param_presence_flags & PARAM_FIR)
if (get_bits1(gbp))
- if (read_filter_params(m, gbp, ch, FIR) < 0)
+ if (read_filter_params(m, gbp, substr, ch, FIR) < 0)
return -1;
if (s->param_presence_flags & PARAM_IIR)
if (get_bits1(gbp))
- if (read_filter_params(m, gbp, ch, IIR) < 0)
+ if (read_filter_params(m, gbp, substr, ch, IIR) < 0)
return -1;
if (fir->order + iir->order > 8) {
return -1;
}
/* The FIR and IIR filters must have the same precision.
- * To simplify the filtering code, only the precision of the
- * FIR filter is considered. If only the IIR filter is employed,
- * the FIR filter precision is set to that of the IIR filter, so
- * that the filtering code can use it. */
+ * To simplify the filtering code, only the precision of the
+ * FIR filter is considered. If only the IIR filter is employed,
+ * the FIR filter precision is set to that of the IIR filter, so
+ * that the filtering code can use it. */
if (!fir->order && iir->order)
fir->shift = iir->shift;
if (s->param_presence_flags & PARAM_QUANTSTEP)
if (get_bits1(gbp))
for (ch = 0; ch <= s->max_channel; ch++) {
- ChannelParams *cp = &m->channel_params[ch];
+ ChannelParams *cp = &s->channel_params[ch];
s->quant_step_size[ch] = get_bits(gbp, 4);
unsigned int channel)
{
SubStream *s = &m->substream[substr];
- int32_t firbuf[MAX_BLOCKSIZE + MAX_FIR_ORDER];
- int32_t iirbuf[MAX_BLOCKSIZE + MAX_IIR_ORDER];
- FilterParams *fir = &m->channel_params[channel].filter_params[FIR];
- FilterParams *iir = &m->channel_params[channel].filter_params[IIR];
+ const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
+ int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
+ int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
+ int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
+ FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
+ FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
unsigned int filter_shift = fir->shift;
int32_t mask = MSB_MASK(s->quant_step_size[channel]);
- int index = MAX_BLOCKSIZE;
- int i;
-
- memcpy(&firbuf[index], fir->state, MAX_FIR_ORDER * sizeof(int32_t));
- memcpy(&iirbuf[index], iir->state, MAX_IIR_ORDER * sizeof(int32_t));
- for (i = 0; i < s->blocksize; i++) {
- int32_t residual = m->sample_buffer[i + s->blockpos][channel];
- unsigned int order;
- int64_t accum = 0;
- int32_t result;
+ memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
+ memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
- /* TODO: Move this code to DSPContext? */
-
- for (order = 0; order < fir->order; order++)
- accum += (int64_t) firbuf[index + order] * fir->coeff[order];
- for (order = 0; order < iir->order; order++)
- accum += (int64_t) iirbuf[index + order] * iir->coeff[order];
-
- accum = accum >> filter_shift;
- result = (accum + residual) & mask;
-
- --index;
-
- firbuf[index] = result;
- iirbuf[index] = result - accum;
-
- m->sample_buffer[i + s->blockpos][channel] = result;
- }
+ m->dsp.mlp_filter_channel(firbuf, fircoeff,
+ fir->order, iir->order,
+ filter_shift, mask, s->blocksize,
+ &m->sample_buffer[s->blockpos][channel]);
- memcpy(fir->state, &firbuf[index], MAX_FIR_ORDER * sizeof(int32_t));
- memcpy(iir->state, &iirbuf[index], MAX_IIR_ORDER * sizeof(int32_t));
+ memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
+ memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
}
/** Read a block of PCM residual data (or actual if no filtering active). */
static int output_data(MLPDecodeContext *m, unsigned int substr,
uint8_t *data, unsigned int *data_size)
{
- if (m->avctx->sample_fmt == SAMPLE_FMT_S32)
+ if (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
return output_data_internal(m, substr, data, data_size, 1);
else
return output_data_internal(m, substr, data, data_size, 0);
/** Read an access unit from the stream.
- * Returns < 0 on error, 0 if not enough data is present in the input stream
- * otherwise returns the number of bytes consumed. */
+ * @return negative on error, 0 if not enough data is present in the input stream,
+ * otherwise the number of bytes consumed. */
static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
AVPacket *avpkt)
length = (AV_RB16(buf) & 0xfff) * 2;
- if (length > buf_size)
+ if (length < 4 || length > buf_size)
return -1;
init_get_bits(&gb, (buf + 4), (length - 4) * 8);
return -1;
}
-#if CONFIG_MLP_DECODER
AVCodec mlp_decoder = {
"mlp",
- CODEC_TYPE_AUDIO,
+ AVMEDIA_TYPE_AUDIO,
CODEC_ID_MLP,
sizeof(MLPDecodeContext),
mlp_decode_init,
read_access_unit,
.long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
};
-#endif /* CONFIG_MLP_DECODER */
#if CONFIG_TRUEHD_DECODER
AVCodec truehd_decoder = {
"truehd",
- CODEC_TYPE_AUDIO,
+ AVMEDIA_TYPE_AUDIO,
CODEC_ID_TRUEHD,
sizeof(MLPDecodeContext),
mlp_decode_init,