summaryrefslogtreecommitdiff
path: root/libavcodec/wmaprodec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/wmaprodec.c')
-rw-r--r--libavcodec/wmaprodec.c504
1 files changed, 398 insertions, 106 deletions
diff --git a/libavcodec/wmaprodec.c b/libavcodec/wmaprodec.c
index daeaa79a7b..5c99628c52 100644
--- a/libavcodec/wmaprodec.c
+++ b/libavcodec/wmaprodec.c
@@ -3,20 +3,20 @@
* Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
* Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -88,6 +88,7 @@
#include <inttypes.h>
+#include "libavutil/ffmath.h"
#include "libavutil/float_dsp.h"
#include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h"
@@ -171,13 +172,13 @@ typedef struct WMAProChannelGrp {
typedef struct WMAProDecodeCtx {
/* generic decoder variables */
AVCodecContext* avctx; ///< codec context for av_log
- AVFloatDSPContext fdsp;
+ AVFloatDSPContext *fdsp;
uint8_t frame_data[MAX_FRAMESIZE +
AV_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
PutBitContext pb; ///< context for filling the frame_data buffer
FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES]; ///< MDCT context per block size
DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
- float* windows[WMAPRO_BLOCK_SIZES]; ///< windows for the different block sizes
+ const float* windows[WMAPRO_BLOCK_SIZES]; ///< windows for the different block sizes
/* frame size dependent frame information (set during initialization) */
uint32_t decode_flags; ///< used compression features
@@ -214,9 +215,11 @@ typedef struct WMAProDecodeCtx {
uint8_t drc_gain; ///< gain for the DRC tool
int8_t skip_frame; ///< skip output step
int8_t parsed_all_subframes; ///< all subframes decoded?
+ uint8_t skip_packets;
/* subframe/block decode state */
int16_t subframe_len; ///< current subframe length
+ int8_t nb_channels; ///< number of channels in stream (XMA1/2)
int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
int8_t num_bands; ///< number of scale factor bands
@@ -231,6 +234,13 @@ typedef struct WMAProDecodeCtx {
WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]; ///< per channel data
} WMAProDecodeCtx;
+typedef struct XMADecodeCtx {
+ WMAProDecodeCtx xma[4];
+ AVFrame *frames[4];
+ int current_stream;
+ float samples[8][512 * 64];
+ int offset[4];
+} XMADecodeCtx;
/**
*@brief helper function to print the most important members of the context
@@ -247,7 +257,7 @@ static av_cold void dump_context(WMAProDecodeCtx *s)
PRINT("log2 frame size", s->log2_frame_size);
PRINT("max num subframes", s->max_num_subframes);
PRINT("len prefix", s->len_prefix);
- PRINT("num channels", s->avctx->channels);
+ PRINT("num channels", s->nb_channels);
}
/**
@@ -255,72 +265,129 @@ static av_cold void dump_context(WMAProDecodeCtx *s)
*@param avctx codec context
*@return 0 on success, < 0 otherwise
*/
-static av_cold int decode_end(AVCodecContext *avctx)
+static av_cold int decode_end(WMAProDecodeCtx *s)
{
- WMAProDecodeCtx *s = avctx->priv_data;
int i;
+ av_freep(&s->fdsp);
+
for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
ff_mdct_end(&s->mdct_ctx[i]);
return 0;
}
+static av_cold int wmapro_decode_end(AVCodecContext *avctx)
+{
+ WMAProDecodeCtx *s = avctx->priv_data;
+
+ decode_end(s);
+
+ return 0;
+}
+
+static av_cold int get_rate(AVCodecContext *avctx)
+{
+ if (avctx->codec_id != AV_CODEC_ID_WMAPRO) { // XXX: is this really only for XMA?
+ if (avctx->sample_rate > 44100)
+ return 48000;
+ else if (avctx->sample_rate > 32000)
+ return 44100;
+ else if (avctx->sample_rate > 24000)
+ return 32000;
+ return 24000;
+ }
+
+ return avctx->sample_rate;
+}
+
/**
*@brief Initialize the decoder.
*@param avctx codec context
*@return 0 on success, -1 otherwise
*/
-static av_cold int decode_init(AVCodecContext *avctx)
+static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx)
{
- WMAProDecodeCtx *s = avctx->priv_data;
uint8_t *edata_ptr = avctx->extradata;
unsigned int channel_mask;
int i, bits;
int log2_max_num_subframes;
int num_possible_block_sizes;
+ if (avctx->codec_id == AV_CODEC_ID_XMA1 || avctx->codec_id == AV_CODEC_ID_XMA2)
+ avctx->block_align = 2048;
+
if (!avctx->block_align) {
av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
return AVERROR(EINVAL);
}
s->avctx = avctx;
- avpriv_float_dsp_init(&s->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
- if (avctx->extradata_size >= 18) {
+ /** dump the extradata */
+ av_log(avctx, AV_LOG_DEBUG, "extradata:\n");
+ for (i = 0; i < avctx->extradata_size; i++)
+ av_log(avctx, AV_LOG_DEBUG, "[%x] ", avctx->extradata[i]);
+ av_log(avctx, AV_LOG_DEBUG, "\n");
+ if (avctx->codec_id == AV_CODEC_ID_XMA2 && (!avctx->extradata || avctx->extradata_size >= 6)) {
+ s->decode_flags = 0x10d6;
+ channel_mask = avctx->extradata ? AV_RL32(edata_ptr+2) : 0;
+ s->bits_per_sample = 16;
+ } else if (avctx->codec_id == AV_CODEC_ID_XMA1) {
+ s->decode_flags = 0x10d6;
+ s->bits_per_sample = 16;
+ channel_mask = 0;
+ } else if (avctx->codec_id == AV_CODEC_ID_WMAPRO && avctx->extradata_size >= 18) {
s->decode_flags = AV_RL16(edata_ptr+14);
channel_mask = AV_RL32(edata_ptr+2);
s->bits_per_sample = AV_RL16(edata_ptr);
- /** dump the extradata */
- for (i = 0; i < avctx->extradata_size; i++)
- ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
- ff_dlog(avctx, "\n");
+ if (s->bits_per_sample > 32 || s->bits_per_sample < 1) {
+ avpriv_request_sample(avctx, "bits per sample is %d", s->bits_per_sample);
+ return AVERROR_PATCHWELCOME;
+ }
} else {
avpriv_request_sample(avctx, "Unknown extradata size");
return AVERROR_PATCHWELCOME;
}
+ if (avctx->codec_id != AV_CODEC_ID_WMAPRO && avctx->channels > 2) {
+ s->nb_channels = 2;
+ } else {
+ s->nb_channels = avctx->channels;
+ }
+
/** generic init */
s->log2_frame_size = av_log2(avctx->block_align) + 4;
+ if (s->log2_frame_size > 25) {
+ avpriv_request_sample(avctx, "Large block align");
+ return AVERROR_PATCHWELCOME;
+ }
/** frame info */
- s->skip_frame = 1; /* skip first frame */
+ if (avctx->codec_id != AV_CODEC_ID_WMAPRO)
+ s->skip_frame = 0;
+ else
+ s->skip_frame = 1; /* skip first frame */
+
s->packet_loss = 1;
s->len_prefix = (s->decode_flags & 0x40);
/** get frame len */
- bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
- if (bits > WMAPRO_BLOCK_MAX_BITS) {
- avpriv_request_sample(avctx, "14-bit block sizes");
- return AVERROR_PATCHWELCOME;
+ if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
+ bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
+ if (bits > WMAPRO_BLOCK_MAX_BITS) {
+ avpriv_request_sample(avctx, "14-bit block sizes");
+ return AVERROR_PATCHWELCOME;
+ }
+ s->samples_per_frame = 1 << bits;
+ } else {
+ s->samples_per_frame = 512;
}
- s->samples_per_frame = 1 << bits;
/** subframe info */
log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
@@ -340,8 +407,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
}
if (s->min_samples_per_subframe < WMAPRO_BLOCK_MIN_SIZE) {
- av_log(avctx, AV_LOG_ERROR, "Invalid minimum block size %"PRId8"\n",
- s->max_num_subframes);
+ av_log(avctx, AV_LOG_ERROR, "min_samples_per_subframe of %d too small\n",
+ s->min_samples_per_subframe);
return AVERROR_INVALIDDATA;
}
@@ -350,18 +417,18 @@ static av_cold int decode_init(AVCodecContext *avctx)
return AVERROR_INVALIDDATA;
}
- if (avctx->channels < 0) {
+ if (s->nb_channels <= 0) {
av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
- avctx->channels);
+ s->nb_channels);
return AVERROR_INVALIDDATA;
- } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
+ } else if (s->nb_channels > WMAPRO_MAX_CHANNELS) {
avpriv_request_sample(avctx,
"More than %d channels", WMAPRO_MAX_CHANNELS);
return AVERROR_PATCHWELCOME;
}
/** init previous block len */
- for (i = 0; i < avctx->channels; i++)
+ for (i = 0; i < s->nb_channels; i++)
s->channel[i].prev_block_len = s->samples_per_frame;
/** extract lfe channel position */
@@ -409,18 +476,25 @@ static av_cold int decode_init(AVCodecContext *avctx)
int subframe_len = s->samples_per_frame >> i;
int x;
int band = 1;
+ int rate = get_rate(avctx);
s->sfb_offsets[i][0] = 0;
for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
- int offset = (subframe_len * 2 * critical_freq[x])
- / s->avctx->sample_rate + 2;
+ int offset = (subframe_len * 2 * critical_freq[x]) / rate + 2;
offset &= ~3;
if (offset > s->sfb_offsets[i][band - 1])
s->sfb_offsets[i][band++] = offset;
+
+ if (offset >= subframe_len)
+ break;
}
s->sfb_offsets[i][band - 1] = subframe_len;
s->num_sfb[i] = band - 1;
+ if (s->num_sfb[i] <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "num_sfb invalid\n");
+ return AVERROR_INVALIDDATA;
+ }
}
@@ -437,14 +511,19 @@ static av_cold int decode_init(AVCodecContext *avctx)
+ s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
for (x = 0; x < num_possible_block_sizes; x++) {
int v = 0;
- while (s->sfb_offsets[x][v + 1] << x < offset)
- if (++v >= MAX_BANDS)
- return AVERROR_INVALIDDATA;
+ while (s->sfb_offsets[x][v + 1] << x < offset) {
+ v++;
+ av_assert0(v < MAX_BANDS);
+ }
s->sf_offsets[i][x][b] = v;
}
}
}
+ s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
+ if (!s->fdsp)
+ return AVERROR(ENOMEM);
+
/** init MDCT, FIXME: only init needed sizes */
for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
@@ -461,7 +540,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
/** calculate subwoofer cutoff values */
for (i = 0; i < num_possible_block_sizes; i++) {
int block_size = s->samples_per_frame >> i;
- int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
+ int cutoff = (440*block_size + 3LL * (s->avctx->sample_rate >> 1) - 1)
/ s->avctx->sample_rate;
s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
}
@@ -479,6 +558,18 @@ static av_cold int decode_init(AVCodecContext *avctx)
}
/**
+ *@brief Initialize the decoder.
+ *@param avctx codec context
+ *@return 0 on success, -1 otherwise
+ */
+static av_cold int wmapro_decode_init(AVCodecContext *avctx)
+{
+ WMAProDecodeCtx *s = avctx->priv_data;
+
+ return decode_init(s, avctx);
+}
+
+/**
*@brief Decode the subframe length.
*@param s context
*@param offset sample offset in the frame
@@ -493,6 +584,9 @@ static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
if (offset == s->samples_per_frame - s->min_samples_per_subframe)
return s->min_samples_per_subframe;
+ if (get_bits_left(&s->gb) < 1)
+ return AVERROR_INVALIDDATA;
+
/** 1 bit indicates if the subframe is of maximum length */
if (s->max_subframe_len_bit) {
if (get_bits1(&s->gb))
@@ -536,7 +630,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
{
uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };/**< sum of samples for all currently known subframes of a channel */
uint8_t contains_subframe[WMAPRO_MAX_CHANNELS]; /**< flag indicating if a channel contains the current subframe */
- int channels_for_cur_subframe = s->avctx->channels; /**< number of channels that contain the current subframe */
+ int channels_for_cur_subframe = s->nb_channels; /**< number of channels that contain the current subframe */
int fixed_channel_layout = 0; /**< flag indicating that all channels use the same subframe offsets and sizes */
int min_channel_len = 0; /**< smallest sum of samples (channels with this length will be processed first) */
int c;
@@ -548,7 +642,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
*/
/** reset tiling information */
- for (c = 0; c < s->avctx->channels; c++)
+ for (c = 0; c < s->nb_channels; c++)
s->channel[c].num_subframes = 0;
if (s->max_num_subframes == 1 || get_bits1(&s->gb))
@@ -559,7 +653,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
int subframe_len;
/** check which channels contain the subframe */
- for (c = 0; c < s->avctx->channels; c++) {
+ for (c = 0; c < s->nb_channels; c++) {
if (num_samples[c] == min_channel_len) {
if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
(min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
@@ -576,7 +670,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
/** add subframes to the individual channels and find new min_channel_len */
min_channel_len += subframe_len;
- for (c = 0; c < s->avctx->channels; c++) {
+ for (c = 0; c < s->nb_channels; c++) {
WMAProChannelCtx* chan = &s->channel[c];
if (contains_subframe[c]) {
@@ -603,11 +697,11 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
}
} while (min_channel_len < s->samples_per_frame);
- for (c = 0; c < s->avctx->channels; c++) {
+ for (c = 0; c < s->nb_channels; c++) {
int i;
int offset = 0;
for (i = 0; i < s->channel[c].num_subframes; i++) {
- ff_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
+ ff_dlog(s->avctx, "frame[%"PRIu32"] channel[%i] subframe[%i]"
" len %i\n", s->frame_num, c, i,
s->channel[c].subframe_len[i]);
s->channel[c].subframe_offset[i] = offset;
@@ -629,8 +723,8 @@ static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
int i;
int offset = 0;
int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
- memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
- s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
+ memset(chgroup->decorrelation_matrix, 0, s->nb_channels *
+ s->nb_channels * sizeof(*chgroup->decorrelation_matrix));
for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
rotation_offset[i] = get_bits(&s->gb, 6);
@@ -671,7 +765,7 @@ static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
/**
*@brief Decode channel transformation parameters
*@param s codec context
- *@return 0 in case of success, < 0 in case of bitstream errors
+ *@return >= 0 in case of success, < 0 in case of bitstream errors
*/
static int decode_channel_transform(WMAProDecodeCtx* s)
{
@@ -683,7 +777,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
/** in the one channel case channel transforms are pointless */
s->num_chgroups = 0;
- if (s->avctx->channels > 1) {
+ if (s->nb_channels > 1) {
int remaining_channels = s->channels_for_cur_subframe;
if (get_bits1(&s->gb)) {
@@ -730,7 +824,7 @@ static int decode_channel_transform(WMAProDecodeCtx* s)
}
} else {
chgroup->transform = 1;
- if (s->avctx->channels == 2) {
+ if (s->nb_channels == 2) {
chgroup->decorrelation_matrix[0] = 1.0;
chgroup->decorrelation_matrix[1] = -1.0;
chgroup->decorrelation_matrix[2] = 1.0;
@@ -1020,12 +1114,12 @@ static void inverse_channel_transform(WMAProDecodeCtx *s)
(*ch)[y] = sum;
}
}
- } else if (s->avctx->channels == 2) {
+ } else if (s->nb_channels == 2) {
int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
- s->fdsp.vector_fmul_scalar(ch_data[0] + sfb[0],
+ s->fdsp->vector_fmul_scalar(ch_data[0] + sfb[0],
ch_data[0] + sfb[0],
181.0 / 128, len);
- s->fdsp.vector_fmul_scalar(ch_data[1] + sfb[0],
+ s->fdsp->vector_fmul_scalar(ch_data[1] + sfb[0],
ch_data[1] + sfb[0],
181.0 / 128, len);
}
@@ -1043,7 +1137,7 @@ static void wmapro_window(WMAProDecodeCtx *s)
int i;
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
- float* window;
+ const float* window;
int winlen = s->channel[c].prev_block_len;
float* start = s->channel[c].coeffs - (winlen >> 1);
@@ -1056,7 +1150,7 @@ static void wmapro_window(WMAProDecodeCtx *s)
winlen >>= 1;
- s->fdsp.vector_fmul_window(start, start, start + winlen,
+ s->fdsp->vector_fmul_window(start, start, start + winlen,
window, winlen);
s->channel[c].prev_block_len = s->subframe_len;
@@ -1073,7 +1167,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
int offset = s->samples_per_frame;
int subframe_len = s->samples_per_frame;
int i;
- int total_samples = s->samples_per_frame * s->avctx->channels;
+ int total_samples = s->samples_per_frame * s->nb_channels;
int transmit_coeffs = 0;
int cur_subwoofer_cutoff;
@@ -1083,7 +1177,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
== the next block of the channel with the smallest number of
decoded samples
*/
- for (i = 0; i < s->avctx->channels; i++) {
+ for (i = 0; i < s->nb_channels; i++) {
s->channel[i].grouped = 0;
if (offset > s->channel[i].decoded_samples) {
offset = s->channel[i].decoded_samples;
@@ -1097,7 +1191,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
/** get a list of all channels that contain the estimated block */
s->channels_for_cur_subframe = 0;
- for (i = 0; i < s->avctx->channels; i++) {
+ for (i = 0; i < s->nb_channels; i++) {
const int cur_subframe = s->channel[i].cur_subframe;
/** subtract already processed samples */
total_samples -= s->channel[i].decoded_samples;
@@ -1175,6 +1269,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
transmit_coeffs = 1;
}
+ av_assert0(s->subframe_len <= WMAPRO_BLOCK_MAX_SIZE);
if (transmit_coeffs) {
int step;
int quant_step = 90 * s->bits_per_sample >> 4;
@@ -1185,10 +1280,11 @@ static int decode_subframe(WMAProDecodeCtx *s)
for (i = 0; i < s->channels_for_cur_subframe; i++) {
int c = s->channel_indexes_for_cur_subframe[i];
int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
- if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) {
+ if (num_vec_coeffs > s->subframe_len) {
av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
return AVERROR_INVALIDDATA;
}
+ av_assert0(num_vec_coeffs + offset <= FF_ARRAY_ELEMS(s->channel[c].out));
s->channel[c].num_vec_coeffs = num_vec_coeffs;
}
} else {
@@ -1272,9 +1368,9 @@ static int decode_subframe(WMAProDecodeCtx *s)
const int exp = s->channel[c].quant_step -
(s->channel[c].max_scale_factor - *sf++) *
s->channel[c].scale_factor_step;
- const float quant = pow(10.0, exp / 20.0);
+ const float quant = ff_exp10(exp / 20.0);
int start = s->cur_sfb_offsets[b];
- s->fdsp.vector_fmul_scalar(s->tmp + start,
+ s->fdsp->vector_fmul_scalar(s->tmp + start,
s->channel[c].coeffs + start,
quant, end - start);
}
@@ -1308,11 +1404,10 @@ static int decode_subframe(WMAProDecodeCtx *s)
*/
static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
{
- AVCodecContext *avctx = s->avctx;
GetBitContext* gb = &s->gb;
int more_frames = 0;
int len = 0;
- int i, ret;
+ int i;
/** get frame length */
if (s->len_prefix)
@@ -1327,9 +1422,9 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
}
/** read postproc transform */
- if (s->avctx->channels > 1 && get_bits1(gb)) {
+ if (s->nb_channels > 1 && get_bits1(gb)) {
if (get_bits1(gb)) {
- for (i = 0; i < avctx->channels * avctx->channels; i++)
+ for (i = 0; i < s->nb_channels * s->nb_channels; i++)
skip_bits(gb, 4);
}
}
@@ -1364,7 +1459,7 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
/** reset subframe states */
s->parsed_all_subframes = 0;
- for (i = 0; i < avctx->channels; i++) {
+ for (i = 0; i < s->nb_channels; i++) {
s->channel[i].decoded_samples = 0;
s->channel[i].cur_subframe = 0;
s->channel[i].reuse_sf = 0;
@@ -1378,20 +1473,12 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
}
}
- /* get output buffer */
- frame->nb_samples = s->samples_per_frame;
- if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- s->packet_loss = 1;
- return 0;
- }
-
/** copy samples to the output buffer */
- for (i = 0; i < avctx->channels; i++)
+ for (i = 0; i < s->nb_channels; i++)
memcpy(frame->extended_data[i], s->channel[i].out,
s->samples_per_frame * sizeof(*s->channel[i].out));
- for (i = 0; i < avctx->channels; i++) {
+ for (i = 0; i < s->nb_channels; i++) {
/** reuse second half of the IMDCT output for the next frame */
memcpy(&s->channel[i].out[0],
&s->channel[i].out[s->samples_per_frame],
@@ -1464,7 +1551,7 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
}
- buflen = (s->num_saved_bits + len + 8) >> 3;
+ buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
if (len <= 0 || buflen > MAX_FRAMESIZE) {
avpriv_request_sample(s->avctx, "Too small input buffer");
@@ -1472,13 +1559,7 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
return;
}
- if (len > put_bits_left(&s->pb)) {
- av_log(s->avctx, AV_LOG_ERROR,
- "Cannot append %d bits, only %d bits available.\n",
- len, put_bits_left(&s->pb));
- s->packet_loss = 1;
- return;
- }
+ av_assert0(len <= put_bits_left(&s->pb));
s->num_saved_bits += len;
if (!append) {
@@ -1502,17 +1583,9 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
skip_bits(&s->gb, s->frame_offset);
}
-/**
- *@brief Decode a single WMA packet.
- *@param avctx codec context
- *@param data the output buffer
- *@param avpkt input packet
- *@return number of bytes that were read from the input buffer
- */
-static int decode_packet(AVCodecContext *avctx, void *data,
- int *got_frame_ptr, AVPacket* avpkt)
+static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s,
+ void *data, int *got_frame_ptr, AVPacket *avpkt)
{
- WMAProDecodeCtx *s = avctx->priv_data;
GetBitContext* gb = &s->pgb;
const uint8_t* buf = avpkt->data;
int buf_size = avpkt->size;
@@ -1525,28 +1598,45 @@ static int decode_packet(AVCodecContext *avctx, void *data,
s->packet_done = 0;
/** sanity check for the buffer length */
- if (buf_size < avctx->block_align) {
+ if (avctx->codec_id == AV_CODEC_ID_WMAPRO && buf_size < avctx->block_align) {
av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
buf_size, avctx->block_align);
return AVERROR_INVALIDDATA;
}
- s->next_packet_start = buf_size - avctx->block_align;
- buf_size = avctx->block_align;
+ if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
+ s->next_packet_start = buf_size - avctx->block_align;
+ buf_size = avctx->block_align;
+ } else {
+ s->next_packet_start = buf_size - FFMIN(buf_size, avctx->block_align);
+ buf_size = FFMIN(buf_size, avctx->block_align);
+ }
s->buf_bit_size = buf_size << 3;
/** parse packet header */
init_get_bits(gb, buf, s->buf_bit_size);
- packet_sequence_number = get_bits(gb, 4);
- skip_bits(gb, 2);
+ if (avctx->codec_id != AV_CODEC_ID_XMA2) {
+ packet_sequence_number = get_bits(gb, 4);
+ skip_bits(gb, 2);
+ } else {
+ int num_frames = get_bits(gb, 6);
+ ff_dlog(avctx, "packet[%d]: number of frames %d\n", avctx->frame_number, num_frames);
+ packet_sequence_number = 0;
+ }
/** get number of bits that need to be added to the previous frame */
num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
+ if (avctx->codec_id != AV_CODEC_ID_WMAPRO) {
+ skip_bits(gb, 3);
+ s->skip_packets = get_bits(gb, 8);
+ ff_dlog(avctx, "packet[%d]: skip packets %d\n", avctx->frame_number, s->skip_packets);
+ }
+
ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
num_bits_prev_frame);
/** check for packet loss */
- if (!s->packet_loss &&
+ if (avctx->codec_id == AV_CODEC_ID_WMAPRO && !s->packet_loss &&
((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
s->packet_loss = 1;
av_log(avctx, AV_LOG_ERROR,
@@ -1583,7 +1673,6 @@ static int decode_packet(AVCodecContext *avctx, void *data,
s->num_saved_bits = 0;
s->packet_loss = 0;
}
-
} else {
int frame_size;
s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
@@ -1593,7 +1682,8 @@ static int decode_packet(AVCodecContext *avctx, void *data,
(frame_size = show_bits(gb, s->log2_frame_size)) &&
frame_size <= remaining_bits(s, gb)) {
save_bits(s, gb, frame_size, 0);
- s->packet_done = !decode_frame(s, data, got_frame_ptr);
+ if (!s->packet_loss)
+ s->packet_done = !decode_frame(s, data, got_frame_ptr);
} else if (!s->len_prefix
&& s->num_saved_bits > get_bits_count(&s->gb)) {
/** when the frames do not have a length prefix, we don't know
@@ -1604,8 +1694,14 @@ static int decode_packet(AVCodecContext *avctx, void *data,
the "previous frame" data from the next packet so that
we get a buffer that only contains full frames */
s->packet_done = !decode_frame(s, data, got_frame_ptr);
- } else
+ } else {
s->packet_done = 1;
+ }
+ }
+
+ if (remaining_bits(s, gb) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -remaining_bits(s, gb));
+ s->packet_loss = 1;
}
if (s->packet_done && !s->packet_loss &&
@@ -1623,19 +1719,186 @@ static int decode_packet(AVCodecContext *avctx, void *data,
}
/**
- *@brief Clear decoder buffers (for seeking).
+ *@brief Decode a single WMA packet.
*@param avctx codec context
+ *@param data the output buffer
+ *@param avpkt input packet
+ *@return number of bytes that were read from the input buffer
*/
-static void flush(AVCodecContext *avctx)
+static int wmapro_decode_packet(AVCodecContext *avctx, void *data,
+ int *got_frame_ptr, AVPacket *avpkt)
{
WMAProDecodeCtx *s = avctx->priv_data;
+ AVFrame *frame = data;
+ int ret;
+
+ /* get output buffer */
+ frame->nb_samples = s->samples_per_frame;
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
+ s->packet_loss = 1;
+ return 0;
+ }
+
+ return decode_packet(avctx, s, data, got_frame_ptr, avpkt);
+}
+
+static int xma_decode_packet(AVCodecContext *avctx, void *data,
+ int *got_frame_ptr, AVPacket *avpkt)
+{
+ XMADecodeCtx *s = avctx->priv_data;
+ int got_stream_frame_ptr = 0;
+ AVFrame *frame = data;
+ int i, ret, offset = INT_MAX;
+
+ ret = decode_packet(avctx, &s->xma[s->current_stream], s->frames[s->current_stream],
+ &got_stream_frame_ptr, avpkt);
+
+ if (got_stream_frame_ptr) {
+ memcpy(&s->samples[s->current_stream * 2 + 0][s->offset[s->current_stream] * 512],
+ s->frames[s->current_stream]->extended_data[0], 512 * 4);
+ if (avctx->channels > 1)
+ memcpy(&s->samples[s->current_stream * 2 + 1][s->offset[s->current_stream] * 512],
+ s->frames[s->current_stream]->extended_data[1], 512 * 4);
+ s->offset[s->current_stream]++;
+ } else if (ret < 0) {
+ memset(s->offset, 0, sizeof(s->offset));
+ s->current_stream = 0;
+ return ret;
+ }
+
+ if (s->xma[s->current_stream].packet_done ||
+ s->xma[s->current_stream].packet_loss) {
+ int bret;
+
+ if (s->xma[s->current_stream].skip_packets == 0) {
+ ;
+ } else if (s->xma[0].skip_packets == 0 && avctx->channels >= 2) {
+ s->current_stream = 0;
+ } else if (s->xma[1].skip_packets == 0 && avctx->channels >= 4) {
+ s->current_stream = 1;
+ } else if (s->xma[2].skip_packets == 0 && avctx->channels >= 6) {
+ s->current_stream = 2;
+ } else if (s->xma[3].skip_packets == 0 && avctx->channels == 8) {
+ s->current_stream = 3;
+ } else {
+ int min[2];
+
+ min[0] = s->xma[0].skip_packets;
+ min[1] = i = 0;
+
+ for (i = 1; i < avctx->channels / 2; i++) {
+ if (s->xma[i].skip_packets < min[0]) {
+ min[1] = i;
+ min[0] = s->xma[i].skip_packets;
+ }
+ }
+
+ s->current_stream = min[1];
+ }
+
+ for (i = 0; i < avctx->channels / 2; i++) {
+ s->xma[i].skip_packets = FFMAX(0, s->xma[i].skip_packets - 1);
+ }
+
+ for (i = 0; i < (avctx->channels + 1) / 2; i++) {
+ offset = FFMIN(offset, s->offset[i]);
+ }
+
+ if (offset > 0) {
+ frame->nb_samples = 512 * offset;
+ if ((bret = ff_get_buffer(avctx, frame, 0)) < 0)
+ return bret;
+
+ for (i = 0; i < (avctx->channels + 1) / 2; i++) {
+ memcpy(frame->extended_data[i * 2 + 0], s->samples[i * 2 + 0], frame->nb_samples * 4);
+ if (avctx->channels > 1)
+ memcpy(frame->extended_data[i * 2 + 1], s->samples[i * 2 + 1], frame->nb_samples * 4);
+ s->offset[i] -= offset;
+ if (s->offset[i]) {
+ memmove(s->samples[i * 2 + 0], s->samples[i * 2 + 0] + frame->nb_samples, s->offset[i] * 4 * 512);
+ if (avctx->channels > 1)
+ memmove(s->samples[i * 2 + 1], s->samples[i * 2 + 1] + frame->nb_samples, s->offset[i] * 4 * 512);
+ }
+ }
+
+ *got_frame_ptr = 1;
+ }
+ }
+
+ return ret;
+}
+
+static av_cold int xma_decode_init(AVCodecContext *avctx)
+{
+ XMADecodeCtx *s = avctx->priv_data;
+ int i, ret;
+
+ if (avctx->channels <= 0 || avctx->channels > 8)
+ return AVERROR_INVALIDDATA;
+
+ for (i = 0; i < (avctx->channels + 1) / 2; i++) {
+ ret = decode_init(&s->xma[i], avctx);
+ if (ret < 0)
+ return ret;
+ s->frames[i] = av_frame_alloc();
+ if (!s->frames[i])
+ return AVERROR(ENOMEM);
+ s->frames[i]->nb_samples = 512;
+ if ((ret = ff_get_buffer(avctx, s->frames[i], 0)) < 0) {
+ return AVERROR(ENOMEM);
+ }
+
+ }
+
+ return ret;
+}
+
+static av_cold int xma_decode_end(AVCodecContext *avctx)
+{
+ XMADecodeCtx *s = avctx->priv_data;
+ int i;
+
+ for (i = 0; i < avctx->channels / 2; i++) {
+ decode_end(&s->xma[i]);
+ av_frame_free(&s->frames[i]);
+ }
+
+ return 0;
+}
+
+static void flush(WMAProDecodeCtx *s)
+{
int i;
/** reset output buffer as a part of it is used during the windowing of a
new frame */
- for (i = 0; i < avctx->channels; i++)
+ for (i = 0; i < s->nb_channels; i++)
memset(s->channel[i].out, 0, s->samples_per_frame *
sizeof(*s->channel[i].out));
s->packet_loss = 1;
+ s->skip_packets = 0;
+}
+
+
+/**
+ *@brief Clear decoder buffers (for seeking).
+ *@param avctx codec context
+ */
+static void wmapro_flush(AVCodecContext *avctx)
+{
+ WMAProDecodeCtx *s = avctx->priv_data;
+
+ flush(s);
+}
+
+static void xma_flush(AVCodecContext *avctx)
+{
+ XMADecodeCtx *s = avctx->priv_data;
+ int i;
+ for (i = 0; i < (avctx->channels + 1) / 2; i++)
+ flush(&s->xma[i]);
+
+ memset(s->offset, 0, sizeof(s->offset));
+ s->current_stream = 0;
}
@@ -1648,11 +1911,40 @@ AVCodec ff_wmapro_decoder = {
.type = AVMEDIA_TYPE_AUDIO,
.id = AV_CODEC_ID_WMAPRO,
.priv_data_size = sizeof(WMAProDecodeCtx),
- .init = decode_init,
- .close = decode_end,
- .decode = decode_packet,
+ .init = wmapro_decode_init,
+ .close = wmapro_decode_end,
+ .decode = wmapro_decode_packet,
+ .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
+ .flush = wmapro_flush,
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+ AV_SAMPLE_FMT_NONE },
+};
+
+AVCodec ff_xma1_decoder = {
+ .name = "xma1",
+ .long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 1"),
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_XMA1,
+ .priv_data_size = sizeof(XMADecodeCtx),
+ .init = xma_decode_init,
+ .close = xma_decode_end,
+ .decode = xma_decode_packet,
+ .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+ AV_SAMPLE_FMT_NONE },
+};
+
+AVCodec ff_xma2_decoder = {
+ .name = "xma2",
+ .long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 2"),
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_XMA2,
+ .priv_data_size = sizeof(XMADecodeCtx),
+ .init = xma_decode_init,
+ .close = xma_decode_end,
+ .decode = xma_decode_packet,
+ .flush = xma_flush,
.capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
- .flush = flush,
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_NONE },
};