summaryrefslogtreecommitdiff
path: root/libavcodec/alsdec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/alsdec.c')
-rw-r--r--libavcodec/alsdec.c497
1 files changed, 416 insertions, 81 deletions
diff --git a/libavcodec/alsdec.c b/libavcodec/alsdec.c
index f356a70104..3986347ee2 100644
--- a/libavcodec/alsdec.c
+++ b/libavcodec/alsdec.c
@@ -1,28 +1,28 @@
/*
* MPEG-4 ALS decoder
- * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ mail.de>
*
- * 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
*/
/**
* @file
* MPEG-4 ALS decoder
- * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
+ * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
*/
#include <inttypes.h>
@@ -35,8 +35,12 @@
#include "bgmc.h"
#include "bswapdsp.h"
#include "internal.h"
+#include "mlz.h"
#include "libavutil/samplefmt.h"
#include "libavutil/crc.h"
+#include "libavutil/softfloat_ieee754.h"
+#include "libavutil/intfloat.h"
+#include "libavutil/intreadwrite.h"
#include <stdint.h>
@@ -199,6 +203,7 @@ typedef struct ALSDecContext {
unsigned int cur_frame_length; ///< length of the current frame to decode
unsigned int frame_id; ///< the frame ID / number of the current frame
unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
+ unsigned int cs_switch; ///< if true, channel rearrangement is done
unsigned int num_blocks; ///< number of blocks used in the current frame
unsigned int s_max; ///< maximum Rice parameter allowed in entropy coding
uint8_t *bgmc_lut; ///< pointer at lookup tables used for BGMC
@@ -224,6 +229,14 @@ typedef struct ALSDecContext {
int32_t **raw_samples; ///< decoded raw samples for each channel
int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
uint8_t *crc_buffer; ///< buffer of byte order corrected samples used for CRC check
+ MLZ* mlz; ///< masked lz decompression structure
+ SoftFloat_IEEE754 *acf; ///< contains common multiplier for all channels
+ int *last_acf_mantissa; ///< contains the last acf mantissa data of common multiplier for all channels
+ int *shift_value; ///< value by which the binary point is to be shifted for all channels
+ int *last_shift_value; ///< contains last shift value for all channels
+ int **raw_mantissa; ///< decoded mantissa bits of the difference signal
+ unsigned char *larray; ///< buffer to store the output of masked lz decompression
+ int *nbits; ///< contains the number of bits to read for masked lz decompression for all samples
} ALSDecContext;
@@ -281,12 +294,14 @@ static av_cold int read_specific_config(ALSDecContext *ctx)
GetBitContext gb;
uint64_t ht_size;
int i, config_offset;
- MPEG4AudioConfig m4ac;
+ MPEG4AudioConfig m4ac = {0};
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
uint32_t als_id, header_size, trailer_size;
+ int ret;
- init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
+ if ((ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size)) < 0)
+ return ret;
config_offset = avpriv_mpeg4audio_get_config(&m4ac, avctx->extradata,
avctx->extradata_size * 8, 1);
@@ -349,16 +364,28 @@ static av_cold int read_specific_config(ALSDecContext *ctx)
if (get_bits_left(&gb) < bits_needed)
return AVERROR_INVALIDDATA;
- if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
+ if (!(sconf->chan_pos = av_malloc_array(avctx->channels, sizeof(*sconf->chan_pos))))
return AVERROR(ENOMEM);
- for (i = 0; i < avctx->channels; i++)
- sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
+ ctx->cs_switch = 1;
+
+ for (i = 0; i < avctx->channels; i++) {
+ sconf->chan_pos[i] = -1;
+ }
+
+ for (i = 0; i < avctx->channels; i++) {
+ int idx;
+
+ idx = get_bits(&gb, chan_pos_bits);
+ if (idx >= avctx->channels || sconf->chan_pos[idx] != -1) {
+ av_log(avctx, AV_LOG_WARNING, "Invalid channel reordering.\n");
+ ctx->cs_switch = 0;
+ break;
+ }
+ sconf->chan_pos[idx] = i;
+ }
align_get_bits(&gb);
- // TODO: use this to actually do channel sorting
- } else {
- sconf->chan_sort = 0;
}
@@ -392,7 +419,7 @@ static av_cold int read_specific_config(ALSDecContext *ctx)
if (get_bits_left(&gb) < 32)
return AVERROR_INVALIDDATA;
- if (avctx->err_recognition & AV_EF_CRCCHECK) {
+ if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {
ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
ctx->crc = 0xFFFFFFFF;
ctx->crc_org = ~get_bits_long(&gb, 32);
@@ -426,9 +453,7 @@ static int check_specific_config(ALSDecContext *ctx)
} \
}
- MISSING_ERR(sconf->floating, "Floating point decoding", AVERROR_PATCHWELCOME);
MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", AVERROR_PATCHWELCOME);
- MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
return error;
}
@@ -551,12 +576,15 @@ static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
/** Read the block data for a constant block
*/
-static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
+static int read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
{
ALSSpecificConfig *sconf = &ctx->sconf;
AVCodecContext *avctx = ctx->avctx;
GetBitContext *gb = &ctx->gb;
+ if (bd->block_length <= 0)
+ return AVERROR_INVALIDDATA;
+
*bd->raw_samples = 0;
*bd->const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
bd->js_blocks = get_bits1(gb);
@@ -571,6 +599,8 @@ static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
// ensure constant block decoding by reusing this field
*bd->const_block = 1;
+
+ return 0;
}
@@ -669,13 +699,17 @@ static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
*bd->opt_order = get_bits(gb, opt_order_length);
if (*bd->opt_order > sconf->max_order) {
*bd->opt_order = sconf->max_order;
- av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\n");
+ av_log(avctx, AV_LOG_ERROR, "Predictor order too large.\n");
return AVERROR_INVALIDDATA;
}
} else {
*bd->opt_order = sconf->max_order;
}
-
+ if (*bd->opt_order > bd->block_length) {
+ *bd->opt_order = bd->block_length;
+ av_log(avctx, AV_LOG_ERROR, "Predictor order too large.\n");
+ return AVERROR_INVALIDDATA;
+ }
opt_order = *bd->opt_order;
if (opt_order) {
@@ -706,7 +740,7 @@ static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
quant_cof[k] = decode_rice(gb, rice_param) + offset;
if (quant_cof[k] < -64 || quant_cof[k] > 63) {
av_log(avctx, AV_LOG_ERROR,
- "quant_cof %"PRIu32" is out of range\n",
+ "quant_cof %"PRId32" is out of range.\n",
quant_cof[k]);
return AVERROR_INVALIDDATA;
}
@@ -742,8 +776,13 @@ static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
- r = get_unary(gb, 0, 3);
+ r = get_unary(gb, 0, 4);
c = get_bits(gb, 2);
+ if (r >= 4) {
+ av_log(avctx, AV_LOG_ERROR, "r overflow\n");
+ return AVERROR_INVALIDDATA;
+ }
+
bd->ltp_gain[2] = ltp_gain_values[r][c];
bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
@@ -844,9 +883,6 @@ static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
*current_res++ = decode_rice(gb, s[sb]);
}
- if (!sconf->mc_coding || ctx->js_switch)
- align_get_bits(gb);
-
return 0;
}
@@ -964,17 +1000,21 @@ static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
*/
static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
{
- int ret = 0;
+ int ret;
GetBitContext *gb = &ctx->gb;
+ ALSSpecificConfig *sconf = &ctx->sconf;
*bd->shift_lsbs = 0;
// read block type flag and read the samples accordingly
if (get_bits1(gb)) {
ret = read_var_block_data(ctx, bd);
} else {
- read_const_block_data(ctx, bd);
+ ret = read_const_block_data(ctx, bd);
}
+ if (!sconf->mc_coding || ctx->js_switch)
+ align_get_bits(gb);
+
return ret;
}
@@ -1026,8 +1066,8 @@ static void zero_remaining(unsigned int b, unsigned int b_max,
{
unsigned int count = 0;
- for (; b < b_max; b++)
- count += div_blocks[b];
+ while (b < b_max)
+ count += div_blocks[b++];
if (count)
memset(buf, 0, sizeof(*buf) * count);
@@ -1132,7 +1172,7 @@ static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
// reconstruct joint-stereo blocks
if (bd[0].js_blocks) {
if (bd[1].js_blocks)
- av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
+ av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair.\n");
for (s = 0; s < div_blocks[b]; s++)
bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
@@ -1180,7 +1220,7 @@ static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
if (current->master_channel >= channels) {
- av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
+ av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel.\n");
return AVERROR_INVALIDDATA;
}
@@ -1205,7 +1245,7 @@ static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
}
if (entries == channels) {
- av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
+ av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data.\n");
return AVERROR_INVALIDDATA;
}
@@ -1238,7 +1278,7 @@ static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
}
if (dep == channels) {
- av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
+ av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation.\n");
return AVERROR_INVALIDDATA;
}
@@ -1253,21 +1293,31 @@ static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
bd->quant_cof = ctx->quant_cof[c];
bd->raw_samples = ctx->raw_samples[c] + offset;
- dep = 0;
- while (!ch[dep].stop_flag) {
+ for (dep = 0; !ch[dep].stop_flag; dep++) {
ptrdiff_t smp;
ptrdiff_t begin = 1;
ptrdiff_t end = bd->block_length - 1;
int64_t y;
int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
+ if (ch[dep].master_channel == c)
+ continue;
+
if (ch[dep].time_diff_flag) {
int t = ch[dep].time_diff_index;
if (ch[dep].time_diff_sign) {
t = -t;
+ if (begin < t) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "begin %"PTRDIFF_SPECIFIER" smaller than time diff index %d.\n", begin, t);
+ return AVERROR_INVALIDDATA;
+ }
begin -= t;
} else {
+ if (end < t) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "end %"PTRDIFF_SPECIFIER" smaller than time diff index %d.\n", end, t);
+ return AVERROR_INVALIDDATA;
+ }
end -= t;
}
@@ -1311,10 +1361,240 @@ static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
bd->raw_samples[smp] += y >> 7;
}
}
+ }
- dep++;
+ return 0;
+}
+
+
+/** multiply two softfloats and handle the rounding off
+ */
+static SoftFloat_IEEE754 multiply(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b) {
+ uint64_t mantissa_temp;
+ uint64_t mask_64;
+ int cutoff_bit_count;
+ unsigned char last_2_bits;
+ unsigned int mantissa;
+ int32_t sign;
+ uint32_t return_val = 0;
+ int bit_count = 48;
+
+ sign = a.sign ^ b.sign;
+
+ // Multiply mantissa bits in a 64-bit register
+ mantissa_temp = (uint64_t)a.mant * (uint64_t)b.mant;
+ mask_64 = (uint64_t)0x1 << 47;
+
+ // Count the valid bit count
+ while (!(mantissa_temp & mask_64) && mask_64) {
+ bit_count--;
+ mask_64 >>= 1;
+ }
+
+ // Round off
+ cutoff_bit_count = bit_count - 24;
+ if (cutoff_bit_count > 0) {
+ last_2_bits = (unsigned char)(((unsigned int)mantissa_temp >> (cutoff_bit_count - 1)) & 0x3 );
+ if ((last_2_bits == 0x3) || ((last_2_bits == 0x1) && ((unsigned int)mantissa_temp & ((0x1UL << (cutoff_bit_count - 1)) - 1)))) {
+ // Need to round up
+ mantissa_temp += (uint64_t)0x1 << cutoff_bit_count;
+ }
+ }
+
+ mantissa = (unsigned int)(mantissa_temp >> cutoff_bit_count);
+
+ // Need one more shift?
+ if (mantissa & 0x01000000ul) {
+ bit_count++;
+ mantissa >>= 1;
+ }
+
+ if (!sign) {
+ return_val = 0x80000000U;
}
+ return_val |= (a.exp + b.exp + bit_count - 47) << 23;
+ return_val |= mantissa;
+ return av_bits2sf_ieee754(return_val);
+}
+
+
+/** Read and decode the floating point sample data
+ */
+static int read_diff_float_data(ALSDecContext *ctx, unsigned int ra_frame) {
+ AVCodecContext *avctx = ctx->avctx;
+ GetBitContext *gb = &ctx->gb;
+ SoftFloat_IEEE754 *acf = ctx->acf;
+ int *shift_value = ctx->shift_value;
+ int *last_shift_value = ctx->last_shift_value;
+ int *last_acf_mantissa = ctx->last_acf_mantissa;
+ int **raw_mantissa = ctx->raw_mantissa;
+ int *nbits = ctx->nbits;
+ unsigned char *larray = ctx->larray;
+ int frame_length = ctx->cur_frame_length;
+ SoftFloat_IEEE754 scale = av_int2sf_ieee754(0x1u, 23);
+ unsigned int partA_flag;
+ unsigned int highest_byte;
+ unsigned int shift_amp;
+ uint32_t tmp_32;
+ int use_acf;
+ int nchars;
+ int i;
+ int c;
+ long k;
+ long nbits_aligned;
+ unsigned long acc;
+ unsigned long j;
+ uint32_t sign;
+ uint32_t e;
+ uint32_t mantissa;
+
+ skip_bits_long(gb, 32); //num_bytes_diff_float
+ use_acf = get_bits1(gb);
+
+ if (ra_frame) {
+ memset(last_acf_mantissa, 0, avctx->channels * sizeof(*last_acf_mantissa));
+ memset(last_shift_value, 0, avctx->channels * sizeof(*last_shift_value) );
+ ff_mlz_flush_dict(ctx->mlz);
+ }
+
+ for (c = 0; c < avctx->channels; ++c) {
+ if (use_acf) {
+ //acf_flag
+ if (get_bits1(gb)) {
+ tmp_32 = get_bits(gb, 23);
+ last_acf_mantissa[c] = tmp_32;
+ } else {
+ tmp_32 = last_acf_mantissa[c];
+ }
+ acf[c] = av_bits2sf_ieee754(tmp_32);
+ } else {
+ acf[c] = FLOAT_1;
+ }
+
+ highest_byte = get_bits(gb, 2);
+ partA_flag = get_bits1(gb);
+ shift_amp = get_bits1(gb);
+
+ if (shift_amp) {
+ shift_value[c] = get_bits(gb, 8);
+ last_shift_value[c] = shift_value[c];
+ } else {
+ shift_value[c] = last_shift_value[c];
+ }
+
+ if (partA_flag) {
+ if (!get_bits1(gb)) { //uncompressed
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i] == 0) {
+ ctx->raw_mantissa[c][i] = get_bits_long(gb, 32);
+ }
+ }
+ } else { //compressed
+ nchars = 0;
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i] == 0) {
+ nchars += 4;
+ }
+ }
+
+ tmp_32 = ff_mlz_decompression(ctx->mlz, gb, nchars, larray);
+ if(tmp_32 != nchars) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Error in MLZ decompression (%d, %d).\n", tmp_32, nchars);
+ return AVERROR_INVALIDDATA;
+ }
+
+ for (i = 0; i < frame_length; ++i) {
+ ctx->raw_mantissa[c][i] = AV_RB32(larray);
+ }
+ }
+ }
+
+ //decode part B
+ if (highest_byte) {
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i] != 0) {
+ //The following logic is taken from Tabel 14.45 and 14.46 from the ISO spec
+ if (av_cmp_sf_ieee754(acf[c], FLOAT_1)) {
+ nbits[i] = 23 - av_log2(abs(ctx->raw_samples[c][i]));
+ } else {
+ nbits[i] = 23;
+ }
+ nbits[i] = FFMIN(nbits[i], highest_byte*8);
+ }
+ }
+
+ if (!get_bits1(gb)) { //uncompressed
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i] != 0) {
+ raw_mantissa[c][i] = get_bitsz(gb, nbits[i]);
+ }
+ }
+ } else { //compressed
+ nchars = 0;
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i]) {
+ nchars += (int) nbits[i] / 8;
+ if (nbits[i] & 7) {
+ ++nchars;
+ }
+ }
+ }
+
+ tmp_32 = ff_mlz_decompression(ctx->mlz, gb, nchars, larray);
+ if(tmp_32 != nchars) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Error in MLZ decompression (%d, %d).\n", tmp_32, nchars);
+ return AVERROR_INVALIDDATA;
+ }
+
+ j = 0;
+ for (i = 0; i < frame_length; ++i) {
+ if (ctx->raw_samples[c][i]) {
+ if (nbits[i] & 7) {
+ nbits_aligned = 8 * ((unsigned int)(nbits[i] / 8) + 1);
+ } else {
+ nbits_aligned = nbits[i];
+ }
+ acc = 0;
+ for (k = 0; k < nbits_aligned/8; ++k) {
+ acc = (acc << 8) + larray[j++];
+ }
+ acc >>= (nbits_aligned - nbits[i]);
+ raw_mantissa[c][i] = acc;
+ }
+ }
+ }
+ }
+
+ for (i = 0; i < frame_length; ++i) {
+ SoftFloat_IEEE754 pcm_sf = av_int2sf_ieee754(ctx->raw_samples[c][i], 0);
+ pcm_sf = av_div_sf_ieee754(pcm_sf, scale);
+
+ if (ctx->raw_samples[c][i] != 0) {
+ if (!av_cmp_sf_ieee754(acf[c], FLOAT_1)) {
+ pcm_sf = multiply(acf[c], pcm_sf);
+ }
+
+ sign = pcm_sf.sign;
+ e = pcm_sf.exp;
+ mantissa = (pcm_sf.mant | 0x800000) + raw_mantissa[c][i];
+
+ while(mantissa >= 0x1000000) {
+ e++;
+ mantissa >>= 1;
+ }
+
+ if (mantissa) e += (shift_value[c] - 127);
+ mantissa &= 0x007fffffUL;
+
+ tmp_32 = (sign << 31) | ((e + EXP_BIAS) << 23) | (mantissa);
+ ctx->raw_samples[c][i] = tmp_32;
+ } else {
+ ctx->raw_samples[c][i] = raw_mantissa[c][i] & 0x007fffffUL;
+ }
+ }
+ align_get_bits(gb);
+ }
return 0;
}
@@ -1387,7 +1667,7 @@ static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
for (c = 0; c < avctx->channels; c++)
if (ctx->chan_data[c] < ctx->chan_data_buffer) {
- av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
+ av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data.\n");
return AVERROR_INVALIDDATA;
}
@@ -1443,6 +1723,7 @@ static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
bd.lpc_cof = ctx->lpc_cof[c];
bd.quant_cof = ctx->quant_cof[c];
bd.raw_samples = ctx->raw_samples[c] + offset;
+
if ((ret = decode_block(ctx, &bd)) < 0)
return ret;
}
@@ -1459,7 +1740,14 @@ static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
sizeof(*ctx->raw_samples[c]) * sconf->max_order);
}
- // TODO: read_diff_float_data
+ if (sconf->floating) {
+ read_diff_float_data(ctx, ra_frame);
+ }
+
+ if (get_bits_left(gb) < 0) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb));
+ return AVERROR_INVALIDDATA;
+ }
return 0;
}
@@ -1478,7 +1766,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
int invalid_frame, ret;
unsigned int c, sample, ra_frame, bytes_read, shift;
- init_get_bits(&ctx->gb, buffer, buffer_size * 8);
+ if ((ret = init_get_bits8(&ctx->gb, buffer, buffer_size)) < 0)
+ return ret;
// In the case that the distance between random access frames is set to zero
// (sconf->ra_distance == 0) no frame is treated as a random access frame.
@@ -1502,19 +1791,23 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
/* get output buffer */
frame->nb_samples = ctx->cur_frame_length;
- if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
return ret;
- }
// transform decoded frame into output format
- #define INTERLEAVE_OUTPUT(bps) \
- { \
- int##bps##_t *dest = (int##bps##_t*)frame->data[0]; \
- shift = bps - ctx->avctx->bits_per_raw_sample; \
- for (sample = 0; sample < ctx->cur_frame_length; sample++) \
- for (c = 0; c < avctx->channels; c++) \
- *dest++ = ctx->raw_samples[c][sample] << shift; \
+ #define INTERLEAVE_OUTPUT(bps) \
+ { \
+ int##bps##_t *dest = (int##bps##_t*)frame->data[0]; \
+ shift = bps - ctx->avctx->bits_per_raw_sample; \
+ if (!ctx->cs_switch) { \
+ for (sample = 0; sample < ctx->cur_frame_length; sample++) \
+ for (c = 0; c < avctx->channels; c++) \
+ *dest++ = ctx->raw_samples[c][sample] << shift; \
+ } else { \
+ for (sample = 0; sample < ctx->cur_frame_length; sample++) \
+ for (c = 0; c < avctx->channels; c++) \
+ *dest++ = ctx->raw_samples[sconf->chan_pos[c]][sample] << shift; \
+ } \
}
if (ctx->avctx->bits_per_raw_sample <= 16) {
@@ -1524,7 +1817,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
}
// update CRC
- if (sconf->crc_enabled && (avctx->err_recognition & AV_EF_CRCCHECK)) {
+ if (sconf->crc_enabled && (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL))) {
int swap = HAVE_BIGENDIAN != sconf->msb_first;
if (ctx->avctx->bits_per_raw_sample == 24) {
@@ -1594,6 +1887,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
static av_cold int decode_end(AVCodecContext *avctx)
{
ALSDecContext *ctx = avctx->priv_data;
+ int i;
av_freep(&ctx->sconf.chan_pos);
@@ -1619,6 +1913,22 @@ static av_cold int decode_end(AVCodecContext *avctx)
av_freep(&ctx->chan_data_buffer);
av_freep(&ctx->reverted_channels);
av_freep(&ctx->crc_buffer);
+ if (ctx->mlz) {
+ av_freep(&ctx->mlz->dict);
+ av_freep(&ctx->mlz);
+ }
+ av_freep(&ctx->acf);
+ av_freep(&ctx->last_acf_mantissa);
+ av_freep(&ctx->shift_value);
+ av_freep(&ctx->last_shift_value);
+ if (ctx->raw_mantissa) {
+ for (i = 0; i < avctx->channels; i++) {
+ av_freep(&ctx->raw_mantissa[i]);
+ }
+ av_freep(&ctx->raw_mantissa);
+ }
+ av_freep(&ctx->larray);
+ av_freep(&ctx->nbits);
return 0;
}
@@ -1681,14 +1991,14 @@ static av_cold int decode_init(AVCodecContext *avctx)
// allocate quantized parcor coefficient buffer
num_buffers = sconf->mc_coding ? avctx->channels : 1;
- ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
- ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers);
- ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
- num_buffers * sconf->max_order);
- ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
- num_buffers * sconf->max_order);
- ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
- sconf->max_order);
+ ctx->quant_cof = av_malloc_array(num_buffers, sizeof(*ctx->quant_cof));
+ ctx->lpc_cof = av_malloc_array(num_buffers, sizeof(*ctx->lpc_cof));
+ ctx->quant_cof_buffer = av_malloc_array(num_buffers * sconf->max_order,
+ sizeof(*ctx->quant_cof_buffer));
+ ctx->lpc_cof_buffer = av_malloc_array(num_buffers * sconf->max_order,
+ sizeof(*ctx->lpc_cof_buffer));
+ ctx->lpc_cof_reversed_buffer = av_malloc_array(sconf->max_order,
+ sizeof(*ctx->lpc_cof_buffer));
if (!ctx->quant_cof || !ctx->lpc_cof ||
!ctx->quant_cof_buffer || !ctx->lpc_cof_buffer ||
@@ -1705,15 +2015,14 @@ static av_cold int decode_init(AVCodecContext *avctx)
}
// allocate and assign lag and gain data buffer for ltp mode
- ctx->const_block = av_malloc (sizeof(*ctx->const_block) * num_buffers);
- ctx->shift_lsbs = av_malloc (sizeof(*ctx->shift_lsbs) * num_buffers);
- ctx->opt_order = av_malloc (sizeof(*ctx->opt_order) * num_buffers);
- ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
- ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
- ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
- ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
- ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
- num_buffers * 5);
+ ctx->const_block = av_malloc_array(num_buffers, sizeof(*ctx->const_block));
+ ctx->shift_lsbs = av_malloc_array(num_buffers, sizeof(*ctx->shift_lsbs));
+ ctx->opt_order = av_malloc_array(num_buffers, sizeof(*ctx->opt_order));
+ ctx->store_prev_samples = av_malloc_array(num_buffers, sizeof(*ctx->store_prev_samples));
+ ctx->use_ltp = av_mallocz_array(num_buffers, sizeof(*ctx->use_ltp));
+ ctx->ltp_lag = av_malloc_array(num_buffers, sizeof(*ctx->ltp_lag));
+ ctx->ltp_gain = av_malloc_array(num_buffers, sizeof(*ctx->ltp_gain));
+ ctx->ltp_gain_buffer = av_malloc_array(num_buffers * 5, sizeof(*ctx->ltp_gain_buffer));
if (!ctx->const_block || !ctx->shift_lsbs ||
!ctx->opt_order || !ctx->store_prev_samples ||
@@ -1729,12 +2038,12 @@ static av_cold int decode_init(AVCodecContext *avctx)
// allocate and assign channel data buffer for mcc mode
if (sconf->mc_coding) {
- ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) *
- num_buffers * num_buffers);
- ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) *
- num_buffers);
- ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
- num_buffers);
+ ctx->chan_data_buffer = av_mallocz_array(num_buffers * num_buffers,
+ sizeof(*ctx->chan_data_buffer));
+ ctx->chan_data = av_mallocz_array(num_buffers,
+ sizeof(*ctx->chan_data));
+ ctx->reverted_channels = av_malloc_array(num_buffers,
+ sizeof(*ctx->reverted_channels));
if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
@@ -1752,9 +2061,35 @@ static av_cold int decode_init(AVCodecContext *avctx)
channel_size = sconf->frame_length + sconf->max_order;
- ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
- ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
- ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
+ ctx->prev_raw_samples = av_malloc_array(sconf->max_order, sizeof(*ctx->prev_raw_samples));
+ ctx->raw_buffer = av_mallocz_array(avctx->channels * channel_size, sizeof(*ctx->raw_buffer));
+ ctx->raw_samples = av_malloc_array(avctx->channels, sizeof(*ctx->raw_samples));
+
+ if (sconf->floating) {
+ ctx->acf = av_malloc_array(avctx->channels, sizeof(*ctx->acf));
+ ctx->shift_value = av_malloc_array(avctx->channels, sizeof(*ctx->shift_value));
+ ctx->last_shift_value = av_malloc_array(avctx->channels, sizeof(*ctx->last_shift_value));
+ ctx->last_acf_mantissa = av_malloc_array(avctx->channels, sizeof(*ctx->last_acf_mantissa));
+ ctx->raw_mantissa = av_mallocz_array(avctx->channels, sizeof(*ctx->raw_mantissa));
+
+ ctx->larray = av_malloc_array(ctx->cur_frame_length * 4, sizeof(*ctx->larray));
+ ctx->nbits = av_malloc_array(ctx->cur_frame_length, sizeof(*ctx->nbits));
+ ctx->mlz = av_mallocz(sizeof(*ctx->mlz));
+
+ if (!ctx->mlz || !ctx->acf || !ctx->shift_value || !ctx->last_shift_value
+ || !ctx->last_acf_mantissa || !ctx->raw_mantissa) {
+ av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ ff_mlz_init_dict(avctx, ctx->mlz);
+ ff_mlz_flush_dict(ctx->mlz);
+
+ for (c = 0; c < avctx->channels; ++c) {
+ ctx->raw_mantissa[c] = av_mallocz_array(ctx->cur_frame_length, sizeof(**ctx->raw_mantissa));
+ }
+ }
// allocate previous raw sample buffer
if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
@@ -1770,11 +2105,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
// allocate crc buffer
if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
- (avctx->err_recognition & AV_EF_CRCCHECK)) {
- ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
- ctx->cur_frame_length *
- avctx->channels *
- av_get_bytes_per_sample(avctx->sample_fmt));
+ (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL))) {
+ ctx->crc_buffer = av_malloc_array(ctx->cur_frame_length *
+ avctx->channels *
+ av_get_bytes_per_sample(avctx->sample_fmt),
+ sizeof(*ctx->crc_buffer));
if (!ctx->crc_buffer) {
av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
ret = AVERROR(ENOMEM);