[FFmpeg-devel] [PATCH 2/4] libavcodec/libavutil: Implementation of AAC_fixed_decoder (LC-module)
Nedeljko Babic
nedeljko.babic at imgtec.com
Wed Sep 18 15:38:31 CEST 2013
From: Mirjana Vulin <mirjana.vulin at imgtec.com>
Signed-off-by: Mirjana Vulin <mirjana.vulin at imgtec.com>
---
libavcodec/Makefile | 15 +-
libavcodec/aac.h | 104 +-
libavcodec/aac_float_emu.h | 699 ++++++++
libavcodec/aacdec.c | 2763 ++------------------------------
libavcodec/aacdec_fixed.c | 458 ++++++
libavcodec/aacdec_template.c | 2747 +++++++++++++++++++++++++++++++
libavcodec/aacdectab.h | 34 +-
libavcodec/aactab.c | 2 +
libavcodec/aactab.h | 2 +
libavcodec/allcodecs.c | 1 +
libavcodec/cbrt_fixed_tablegen.c | 24 +
libavcodec/cbrt_tablegen.c | 17 +-
libavcodec/cbrt_tablegen.h | 17 +-
libavcodec/cbrt_tablegen_template.c | 37 +
libavcodec/dsputil.c | 12 +
libavcodec/dsputil.h | 17 +
libavcodec/float_emu.h | 400 +++++
libavcodec/float_emu_tab.c | 296 ++++
libavcodec/fmtconvert.c | 19 +-
libavcodec/fmtconvert.h | 2 +
libavcodec/lpc.h | 48 +
libavcodec/mdct.c | 5 +
libavcodec/mips/Makefile | 10 +-
libavcodec/mips/aacdec_mips.c | 6 +-
libavcodec/mips/fmtconvert_mips.c | 4 +
libavcodec/sinewin.c | 1 +
libavcodec/sinewin.h | 20 +-
libavcodec/sinewin_fixed.c | 21 +
libavcodec/sinewin_fixed_tablegen.c | 24 +
libavcodec/sinewin_tablegen.c | 26 +-
libavcodec/sinewin_tablegen.h | 31 +-
libavcodec/sinewin_tablegen_template.c | 60 +
libavcodec/tableprint.h | 2 +
libavutil/fixed_dsp.c | 63 +-
libavutil/fixed_dsp.h | 53 +
35 files changed, 5269 insertions(+), 2771 deletions(-)
create mode 100644 libavcodec/aac_float_emu.h
create mode 100644 libavcodec/aacdec_fixed.c
create mode 100644 libavcodec/aacdec_template.c
create mode 100644 libavcodec/cbrt_fixed_tablegen.c
create mode 100644 libavcodec/cbrt_tablegen_template.c
create mode 100644 libavcodec/float_emu.h
create mode 100644 libavcodec/float_emu_tab.c
create mode 100644 libavcodec/sinewin_fixed.c
create mode 100644 libavcodec/sinewin_fixed_tablegen.c
create mode 100644 libavcodec/sinewin_tablegen_template.c
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c3f8c49..9c0a136 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -21,7 +21,9 @@ OBJS = allcodecs.o \
bitstream_filter.o \
codec_desc.o \
fmtconvert.o \
+ float_emu_tab.o \
imgconvert.o \
+ log2_tab.o \
mathtables.o \
options.o \
parser.o \
@@ -73,7 +75,7 @@ OBJS-$(CONFIG_RANGECODER) += rangecoder.o
RDFT-OBJS-$(CONFIG_HARDCODED_TABLES) += sin_tables.o
OBJS-$(CONFIG_RDFT) += rdft.o $(RDFT-OBJS-yes)
OBJS-$(CONFIG_SHARED) += log2_tab.o
-OBJS-$(CONFIG_SINEWIN) += sinewin.o
+OBJS-$(CONFIG_SINEWIN) += sinewin.o sinewin_fixed.o
OBJS-$(CONFIG_VAAPI) += vaapi.o
OBJS-$(CONFIG_VDPAU) += vdpau.o
OBJS-$(CONFIG_VIDEODSP) += videodsp.o
@@ -86,6 +88,8 @@ OBJS-$(CONFIG_A64MULTI5_ENCODER) += a64multienc.o elbg.o
OBJS-$(CONFIG_AAC_DECODER) += aacdec.o aactab.o aacsbr.o aacps.o \
aacadtsdec.o mpeg4audio.o kbdwin.o \
sbrdsp.o aacpsdsp.o
+OBJS-$(CONFIG_AAC_FIXED_DECODER) += aacdec_fixed.o aactab.o \
+ aacadtsdec.o mpeg4audio.o kbdwin.o
OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o \
aacpsy.o aactab.o \
psymodel.o iirfilter.o \
@@ -836,6 +840,7 @@ TOOLS = fourcc2pixfmt
HOSTPROGS = aac_tablegen \
aacps_tablegen \
cbrt_tablegen \
+ cbrt_fixed_tablegen \
cos_tablegen \
dv_tablegen \
motionpixels_tablegen \
@@ -843,6 +848,7 @@ HOSTPROGS = aac_tablegen \
pcm_tablegen \
qdm2_tablegen \
sinewin_tablegen \
+ sinewin_fixed_tablegen \
CLEANFILES = *_tables.c *_tables.h *_tablegen$(HOSTEXESUF)
@@ -860,8 +866,8 @@ else
$(SUBDIR)%_tablegen$(HOSTEXESUF): HOSTCFLAGS += -DCONFIG_SMALL=0
endif
-GEN_HEADERS = cbrt_tables.h aacps_tables.h aac_tables.h dv_tables.h \
- sinewin_tables.h mpegaudio_tables.h motionpixels_tables.h \
+GEN_HEADERS = cbrt_tables.h cbrt_fixed_tables.h aacps_tables.h aac_tables.h dv_tables.h \
+ sinewin_tables.h sinewin_fixed_tables.h mpegaudio_tables.h motionpixels_tables.h \
pcm_tables.h qdm2_tables.h
GEN_HEADERS := $(addprefix $(SUBDIR), $(GEN_HEADERS))
@@ -870,10 +876,13 @@ $(GEN_HEADERS): $(SUBDIR)%_tables.h: $(SUBDIR)%_tablegen$(HOSTEXESUF)
ifdef CONFIG_HARDCODED_TABLES
$(SUBDIR)aacdec.o: $(SUBDIR)cbrt_tables.h
+$(SUBDIR)aacdec_fixed.o: $(SUBDIR)cbrt_fixed_tables.h
$(SUBDIR)aacps.o: $(SUBDIR)aacps_tables.h
$(SUBDIR)aactab.o: $(SUBDIR)aac_tables.h
+$(SUBDIR)aactab_fixed.o: $(SUBDIR)aac_fixed_tables.h
$(SUBDIR)dv.o: $(SUBDIR)dv_tables.h
$(SUBDIR)sinewin.o: $(SUBDIR)sinewin_tables.h
+$(SUBDIR)sinewin_fixed.o: $(SUBDIR)sinewin_fixed_tables.h
$(SUBDIR)mpegaudiodec.o: $(SUBDIR)mpegaudio_tables.h
$(SUBDIR)mpegaudiodec_float.o: $(SUBDIR)mpegaudio_tables.h
$(SUBDIR)motionpixels.o: $(SUBDIR)motionpixels_tables.h
diff --git a/libavcodec/aac.h b/libavcodec/aac.h
index 209c715..c4b4ba0 100644
--- a/libavcodec/aac.h
+++ b/libavcodec/aac.h
@@ -30,8 +30,62 @@
#ifndef AVCODEC_AAC_H
#define AVCODEC_AAC_H
+#ifndef CONFIG_AAC_FIXED
+#define CONFIG_AAC_FIXED 0
+#endif
+
+#if CONFIG_AAC_FIXED
+
+#define CONFIG_FFT_FLOAT 0
+#define CONFIG_FFT_FIXED_32 1
+
+#define AAC_RENAME(x) x ## _fixed
+#define AAC_RENAME2(x) x ## _fixed
+#define AAC_RENAME_32(x) x ## _fixed_32
+#define INTFLOAT int
+#define SHORTFLOAT int16_t
+#define AAC_FLOAT aac_float_t
+#define AAC_SIGNE int
+#define FIXR(a) ((int)((a) * 1 + 0.5))
+#define FIXR10(a) ((int)((a) * 1024.0 + 0.5))
+#define Q23(a) (int)((a) * 8388608.0 + 0.5)
+#define Q30(x) (int)((x)*1073741824.0 + 0.5)
+#define Q31(x) (int)((x)*2147483648.0 + 0.5)
+#define RANGE15(x) x
+#define GET_GAIN(x, y) (-(y) << (x)) + 1024
+#define AAC_MUL26(x, y) (int)(((int64_t)(x) * (y) + 0x2000000) >> 26)
+#define AAC_MUL30(x, y) (int)(((int64_t)(x) * (y) + 0x20000000) >> 30)
+#define AAC_MUL31(x, y) (int)(((int64_t)(x) * (y) + 0x40000000) >> 31)
+
+#else
+
+#define CONFIG_FFT_FLOAT 1
+#define CONFIG_FFT_FIXED_32 0
+
+#define AAC_RENAME(x) x
+#define AAC_RENAME2(x) x ## _float
+#define AAC_RENAME_32(x) x
+#define INTFLOAT float
+#define SHORTFLOAT float
+#define AAC_FLOAT float
+#define AAC_SIGNE unsigned
+#define FIXR(x) ((float)(x))
+#define FIXR10(x) ((float)(x))
+#define Q23(x) x
+#define Q30(x) x
+#define Q31(x) x
+#define RANGE15(x) (32768.0 * (x))
+#define GET_GAIN(x, y) powf((x), -(y))
+#define AAC_MUL26(x, y) ((x) * (y))
+#define AAC_MUL30(x, y) ((x) * (y))
+#define AAC_MUL31(x, y) ((x) * (y))
+
+#endif /* CONFIG_AAC_FIXED */
+
#include "libavutil/float_dsp.h"
+#include "libavutil/fixed_dsp.h"
#include "avcodec.h"
+#include "dsputil.h"
#include "fft.h"
#include "mpeg4audio.h"
#include "sbr.h"
@@ -125,12 +179,12 @@ typedef struct OutputConfiguration {
* Predictor State
*/
typedef struct PredictorState {
- float cor0;
- float cor1;
- float var0;
- float var1;
- float r0;
- float r1;
+ AAC_FLOAT cor0;
+ AAC_FLOAT cor1;
+ AAC_FLOAT var0;
+ AAC_FLOAT var1;
+ AAC_FLOAT r0;
+ AAC_FLOAT r1;
} PredictorState;
#define MAX_PREDICTORS 672
@@ -147,7 +201,7 @@ typedef struct PredictorState {
typedef struct LongTermPrediction {
int8_t present;
int16_t lag;
- float coef;
+ INTFLOAT coef;
int8_t used[MAX_LTP_LONG_SFB];
} LongTermPrediction;
@@ -181,7 +235,7 @@ typedef struct TemporalNoiseShaping {
int length[8][4];
int direction[8][4];
int order[8][4];
- float coef[8][4][TNS_MAX_ORDER];
+ INTFLOAT coef[8][4][TNS_MAX_ORDER];
} TemporalNoiseShaping;
/**
@@ -218,7 +272,7 @@ typedef struct ChannelCoupling {
int ch_select[8]; /**< [0] shared list of gains; [1] list of gains for right channel;
* [2] list of gains for left channel; [3] lists of gains for both channels
*/
- float gain[16][120];
+ INTFLOAT gain[16][120];
} ChannelCoupling;
/**
@@ -230,15 +284,16 @@ typedef struct SingleChannelElement {
Pulse pulse;
enum BandType band_type[128]; ///< band types
int band_type_run_end[120]; ///< band type run end points
- float sf[120]; ///< scalefactors
+ INTFLOAT sf[120]; ///< scalefactors
int sf_idx[128]; ///< scalefactor indices (used by encoder)
uint8_t zeroes[128]; ///< band is not coded (used by encoder)
- DECLARE_ALIGNED(32, float, coeffs)[1024]; ///< coefficients for IMDCT
- DECLARE_ALIGNED(32, float, saved)[1024]; ///< overlap
- DECLARE_ALIGNED(32, float, ret_buf)[2048]; ///< PCM output buffer
- DECLARE_ALIGNED(16, float, ltp_state)[3072]; ///< time signal for LTP
+ DECLARE_ALIGNED(32, INTFLOAT, coeffs)[1024]; ///< coefficients for IMDCT
+ DECLARE_ALIGNED(32, INTFLOAT, saved)[1024]; ///< overlap
+ DECLARE_ALIGNED(32, INTFLOAT, ret_buf)[2048]; ///< PCM output buffer
+ DECLARE_ALIGNED(32, int, temp_sbr)[2048]; ///< PCM intermediate buffer for SBR
+ DECLARE_ALIGNED(16, INTFLOAT, ltp_state)[3072]; ///< time signal for LTP
PredictorState predictor_state[MAX_PREDICTORS];
- float *ret; ///< PCM output
+ INTFLOAT *ret; ///< PCM output
} SingleChannelElement;
/**
@@ -281,7 +336,7 @@ struct AACContext {
* (We do not want to have these on the stack.)
* @{
*/
- DECLARE_ALIGNED(32, float, buf_mdct)[1024];
+ DECLARE_ALIGNED(32, INTFLOAT, buf_mdct)[1024];
/** @} */
/**
@@ -291,8 +346,13 @@ struct AACContext {
FFTContext mdct;
FFTContext mdct_small;
FFTContext mdct_ltp;
+ DSPContext dsp;
FmtConvertContext fmt_conv;
+#if CONFIG_AAC_FIXED
+ AVFixedDSPContext fdsp;
+#else
AVFloatDSPContext fdsp;
+#endif /* CONFIG_AAC_FIXED */
int random_state;
/** @} */
@@ -312,7 +372,7 @@ struct AACContext {
int dmono_mode; ///< 0->not dmono, 1->use first channel, 2->use second channel
/** @} */
- DECLARE_ALIGNED(32, float, temp)[128];
+ DECLARE_ALIGNED(32, INTFLOAT, temp)[128];
OutputConfiguration oc[2];
int warned_num_aac_frames;
@@ -320,14 +380,18 @@ struct AACContext {
/* aacdec functions pointers */
void (*imdct_and_windowing)(AACContext *ac, SingleChannelElement *sce);
void (*apply_ltp)(AACContext *ac, SingleChannelElement *sce);
- void (*apply_tns)(float coef[1024], TemporalNoiseShaping *tns,
+ void (*apply_tns)(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
IndividualChannelStream *ics, int decode);
- void (*windowing_and_mdct_ltp)(AACContext *ac, float *out,
- float *in, IndividualChannelStream *ics);
+ void (*windowing_and_mdct_ltp)(AACContext *ac, INTFLOAT *out,
+ INTFLOAT *in, IndividualChannelStream *ics);
void (*update_ltp)(AACContext *ac, SingleChannelElement *sce);
+ void (*vector_pow43)(int *coefs, int len);
+ void (*subband_scale)(int *dst, int *src, int scale, int offset, int len);
+ void (*imdct_and_windowing_fixed)(AACContext *ac, SingleChannelElement *sce);
};
+extern int exp2tab[4];
void ff_aacdec_init_mips(AACContext *c);
#endif /* AVCODEC_AAC_H */
diff --git a/libavcodec/aac_float_emu.h b/libavcodec/aac_float_emu.h
new file mode 100644
index 0000000..bbce0f4
--- /dev/null
+++ b/libavcodec/aac_float_emu.h
@@ -0,0 +1,699 @@
+/*
+ * Copyright (c) 2012
+ * MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of is
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author: Stanislav Ocovaj (stanislav.ocovaj imgtec com)
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if !defined(_AAC_FLOAT_EMU_)
+
+#define _AAC_FLOAT_EMU_
+
+#include "libavutil/common.h"
+
+typedef struct aac_float_t {
+ int mant;
+ int expo;
+} aac_float_t;
+
+#define ADD_SUFFIX(a) a ## _fixed
+#define Q30(x) (int)((x)*1073741824.0 + 0.5)
+#define Q31(x) (int)((x)*2147483648.0 + 0.5)
+
+#define ff_log2_tab ADD_SUFFIX(ff_log2_tab)
+#define divTable ADD_SUFFIX(divTable)
+#define sqrtTab ADD_SUFFIX(sqrtTab)
+#define sqrExpMultTab ADD_SUFFIX(sqrExpMultTab)
+#define aac_costbl_1 ADD_SUFFIX(aac_costbl_1)
+#define aac_costbl_2 ADD_SUFFIX(aac_costbl_2)
+#define aac_sintbl_2 ADD_SUFFIX(aac_sintbl_2)
+#define aac_costbl_3 ADD_SUFFIX(aac_costbl_3)
+#define aac_sintbl_3 ADD_SUFFIX(aac_sintbl_3)
+#define aac_costbl_4 ADD_SUFFIX(aac_costbl_4)
+#define aac_sintbl_4 ADD_SUFFIX(aac_sintbl_4)
+
+static const aac_float_t FLOAT_0 = { 0, 0};
+static const aac_float_t FLOAT_05 = { 536870912, 0};
+static const aac_float_t FLOAT_1 = { 536870912, 1};
+static const aac_float_t FLOAT_EPSILON = { 703687442, -16};
+static const aac_float_t FLOAT_1584893192 = { 850883053, 1};
+static const aac_float_t FLOAT_100000 = { 819200000, 17};
+static const aac_float_t FLOAT_0999999 = {1073740750, 0};
+
+static const uint8_t ff_log2_tab[256] = {
+ 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
+};
+
+static int divTable[128] = {
+ Q30(0.9999999995), Q30(0.9922480620), Q30(0.9846153846), Q30(0.9770992366),
+ Q30(0.9696969697), Q30(0.9624060150), Q30(0.9552238807), Q30(0.9481481481),
+ Q30(0.9411764704), Q30(0.9343065694), Q30(0.9275362319), Q30(0.9208633094),
+ Q30(0.9142857143), Q30(0.9078014186), Q30(0.9014084507), Q30(0.8951048949),
+ Q30(0.8888888890), Q30(0.8827586207), Q30(0.8767123288), Q30(0.8707482992),
+ Q30(0.8648648649), Q30(0.8590604025), Q30(0.8533333335), Q30(0.8476821193),
+ Q30(0.8421052634), Q30(0.8366013071), Q30(0.8311688313), Q30(0.8258064515),
+ Q30(0.8205128205), Q30(0.8152866242), Q30(0.8101265822), Q30(0.8050314467),
+ Q30(0.7999999998), Q30(0.7950310558), Q30(0.7901234566), Q30(0.7852760735),
+ Q30(0.7804878047), Q30(0.7757575759), Q30(0.7710843375), Q30(0.7664670660),
+ Q30(0.7619047621), Q30(0.7573964498), Q30(0.7529411763), Q30(0.7485380117),
+ Q30(0.7441860465), Q30(0.7398843933), Q30(0.7356321840), Q30(0.7314285715),
+ Q30(0.7272727271), Q30(0.7231638418), Q30(0.7191011235), Q30(0.7150837989),
+ Q30(0.7111111111), Q30(0.7071823203), Q30(0.7032967033), Q30(0.6994535518),
+ Q30(0.6956521738), Q30(0.6918918919), Q30(0.6881720428), Q30(0.6844919785),
+ Q30(0.6808510637), Q30(0.6772486772), Q30(0.6736842105), Q30(0.6701570679),
+ Q30(0.6666666665), Q30(0.6632124353), Q30(0.6597938146), Q30(0.6564102564),
+ Q30(0.6530612246), Q30(0.6497461931), Q30(0.6464646463), Q30(0.6432160805),
+ Q30(0.6400000001), Q30(0.6368159205), Q30(0.6336633665), Q30(0.6305418718),
+ Q30(0.6274509802), Q30(0.6243902440), Q30(0.6213592235), Q30(0.6183574880),
+ Q30(0.6153846155), Q30(0.6124401912), Q30(0.6095238095), Q30(0.6066350709),
+ Q30(0.6037735851), Q30(0.6009389670), Q30(0.5981308413), Q30(0.5953488373),
+ Q30(0.5925925928), Q30(0.5898617511), Q30(0.5871559633), Q30(0.5844748858),
+ Q30(0.5818181820), Q30(0.5791855203), Q30(0.5765765766), Q30(0.5739910314),
+ Q30(0.5714285714), Q30(0.5688888887), Q30(0.5663716816), Q30(0.5638766522),
+ Q30(0.5614035088), Q30(0.5589519651), Q30(0.5565217393), Q30(0.5541125541),
+ Q30(0.5517241377), Q30(0.5493562231), Q30(0.5470085470), Q30(0.5446808510),
+ Q30(0.5423728814), Q30(0.5400843881), Q30(0.5378151261), Q30(0.5355648533),
+ Q30(0.5333333332), Q30(0.5311203320), Q30(0.5289256200), Q30(0.5267489711),
+ Q30(0.5245901640), Q30(0.5224489798), Q30(0.5203252034), Q30(0.5182186235),
+ Q30(0.5161290322), Q30(0.5140562248), Q30(0.5120000001), Q30(0.5099601592),
+ Q30(0.5079365079), Q30(0.5059288535), Q30(0.5039370079), Q30(0.5019607842)
+};
+
+static int sqrtTab[512+1] = { /* sqrt(x), 0.5<=x<1 */
+ Q30(0.7071067812), Q30(0.7077969783), Q30(0.7084865030), Q30(0.7091753576),
+ Q30(0.7098635430), Q30(0.7105510626), Q30(0.7112379172), Q30(0.7119241091),
+ Q30(0.7126096408), Q30(0.7132945131), Q30(0.7139787287), Q30(0.7146622892),
+ Q30(0.7153451964), Q30(0.7160274521), Q30(0.7167090587), Q30(0.7173900176),
+ Q30(0.7180703310), Q30(0.7187500000), Q30(0.7194290273), Q30(0.7201074138),
+ Q30(0.7207851619), Q30(0.7214622740), Q30(0.7221387504), Q30(0.7228145939),
+ Q30(0.7234898065), Q30(0.7241643891), Q30(0.7248383439), Q30(0.7255116729),
+ Q30(0.7261843774), Q30(0.7268564594), Q30(0.7275279206), Q30(0.7281987625),
+ Q30(0.7288689869), Q30(0.7295385958), Q30(0.7302075904), Q30(0.7308759727),
+ Q30(0.7315437444), Q30(0.7322109072), Q30(0.7328774626), Q30(0.7335434123),
+ Q30(0.7342087580), Q30(0.7348735011), Q30(0.7355376435), Q30(0.7362011867),
+ Q30(0.7368641328), Q30(0.7375264824), Q30(0.7381882383), Q30(0.7388494010),
+ Q30(0.7395099727), Q30(0.7401699550), Q30(0.7408293495), Q30(0.7414881573),
+ Q30(0.7421463802), Q30(0.7428040202), Q30(0.7434610785), Q30(0.7441175561),
+ Q30(0.7447734554), Q30(0.7454287778), Q30(0.7460835241), Q30(0.7467376967),
+ Q30(0.7473912966), Q30(0.7480443250), Q30(0.7486967845), Q30(0.7493486754),
+ Q30(0.7500000000), Q30(0.7506507593), Q30(0.7513009552), Q30(0.7519505885),
+ Q30(0.7525996612), Q30(0.7532481747), Q30(0.7538961302), Q30(0.7545435294),
+ Q30(0.7551903734), Q30(0.7558366638), Q30(0.7564824023), Q30(0.7571275900),
+ Q30(0.7577722282), Q30(0.7584163188), Q30(0.7590598627), Q30(0.7597028613),
+ Q30(0.7603453165), Q30(0.7609872287), Q30(0.7616286003), Q30(0.7622694322),
+ Q30(0.7629097258), Q30(0.7635494824), Q30(0.7641887036), Q30(0.7648273907),
+ Q30(0.7654655445), Q30(0.7661031671), Q30(0.7667402592), Q30(0.7673768224),
+ Q30(0.7680128580), Q30(0.7686483674), Q30(0.7692833515), Q30(0.7699178122),
+ Q30(0.7705517504), Q30(0.7711851676), Q30(0.7718180646), Q30(0.7724504434),
+ Q30(0.7730823047), Q30(0.7737136502), Q30(0.7743444811), Q30(0.7749747979),
+ Q30(0.7756046029), Q30(0.7762338966), Q30(0.7768626809), Q30(0.7774909567),
+ Q30(0.7781187249), Q30(0.7787459870), Q30(0.7793727447), Q30(0.7799989986),
+ Q30(0.7806247496), Q30(0.7812500000), Q30(0.7818747503), Q30(0.7824990018),
+ Q30(0.7831227556), Q30(0.7837460130), Q30(0.7843687749), Q30(0.7849910432),
+ Q30(0.7856128183), Q30(0.7862341017), Q30(0.7868548944), Q30(0.7874751980),
+ Q30(0.7880950132), Q30(0.7887143414), Q30(0.7893331838), Q30(0.7899515415),
+ Q30(0.7905694149), Q30(0.7911868063), Q30(0.7918037162), Q30(0.7924201456),
+ Q30(0.7930360963), Q30(0.7936515687), Q30(0.7942665643), Q30(0.7948810840),
+ Q30(0.7954951287), Q30(0.7961087003), Q30(0.7967217988), Q30(0.7973344265),
+ Q30(0.7979465835), Q30(0.7985582710), Q30(0.7991694906), Q30(0.7997802431),
+ Q30(0.8003905294), Q30(0.8010003511), Q30(0.8016097085), Q30(0.8022186034),
+ Q30(0.8028270360), Q30(0.8034350080), Q30(0.8040425205), Q30(0.8046495742),
+ Q30(0.8052561702), Q30(0.8058623099), Q30(0.8064679937), Q30(0.8070732229),
+ Q30(0.8076779991), Q30(0.8082823223), Q30(0.8088861941), Q30(0.8094896153),
+ Q30(0.8100925875), Q30(0.8106951108), Q30(0.8112971867), Q30(0.8118988159),
+ Q30(0.8125000000), Q30(0.8131007394), Q30(0.8137010355), Q30(0.8143008887),
+ Q30(0.8149003005), Q30(0.8154992717), Q30(0.8160978034), Q30(0.8166958964),
+ Q30(0.8172935518), Q30(0.8178907707), Q30(0.8184875534), Q30(0.8190839016),
+ Q30(0.8196798153), Q30(0.8202752969), Q30(0.8208703459), Q30(0.8214649642),
+ Q30(0.8220591522), Q30(0.8226529113), Q30(0.8232462420), Q30(0.8238391452),
+ Q30(0.8244316224), Q30(0.8250236739), Q30(0.8256153008), Q30(0.8262065044),
+ Q30(0.8267972847), Q30(0.8273876435), Q30(0.8279775814), Q30(0.8285670988),
+ Q30(0.8291561976), Q30(0.8297448778), Q30(0.8303331411), Q30(0.8309209873),
+ Q30(0.8315084185), Q30(0.8320954349), Q30(0.8326820373), Q30(0.8332682266),
+ Q30(0.8338540038), Q30(0.8344393703), Q30(0.8350243261), Q30(0.8356088721),
+ Q30(0.8361930102), Q30(0.8367767399), Q30(0.8373600631), Q30(0.8379429798),
+ Q30(0.8385254918), Q30(0.8391075991), Q30(0.8396893027), Q30(0.8402706035),
+ Q30(0.8408515030), Q30(0.8414320010), Q30(0.8420120990), Q30(0.8425917975),
+ Q30(0.8431710978), Q30(0.8437500000), Q30(0.8443285055), Q30(0.8449066146),
+ Q30(0.8454843285), Q30(0.8460616483), Q30(0.8466385738), Q30(0.8472151072),
+ Q30(0.8477912480), Q30(0.8483669977), Q30(0.8489423566), Q30(0.8495173263),
+ Q30(0.8500919067), Q30(0.8506660992), Q30(0.8512399043), Q30(0.8518133233),
+ Q30(0.8523863559), Q30(0.8529590038), Q30(0.8535312680), Q30(0.8541031480),
+ Q30(0.8546746457), Q30(0.8552457616), Q30(0.8558164961), Q30(0.8563868506),
+ Q30(0.8569568251), Q30(0.8575264211), Q30(0.8580956385), Q30(0.8586644791),
+ Q30(0.8592329426), Q30(0.8598010307), Q30(0.8603687435), Q30(0.8609360820),
+ Q30(0.8615030469), Q30(0.8620696389), Q30(0.8626358588), Q30(0.8632017071),
+ Q30(0.8637671852), Q30(0.8643322927), Q30(0.8648970313), Q30(0.8654614016),
+ Q30(0.8660254036), Q30(0.8665890391), Q30(0.8671523076), Q30(0.8677152111),
+ Q30(0.8682777495), Q30(0.8688399233), Q30(0.8694017339), Q30(0.8699631817),
+ Q30(0.8705242672), Q30(0.8710849914), Q30(0.8716453551), Q30(0.8722053585),
+ Q30(0.8727650028), Q30(0.8733242881), Q30(0.8738832157), Q30(0.8744417862),
+ Q30(0.8750000000), Q30(0.8755578580), Q30(0.8761153608), Q30(0.8766725087),
+ Q30(0.8772293031), Q30(0.8777857441), Q30(0.8783418327), Q30(0.8788975696),
+ Q30(0.8794529550), Q30(0.8800079902), Q30(0.8805626752), Q30(0.8811170114),
+ Q30(0.8816709989), Q30(0.8822246385), Q30(0.8827779307), Q30(0.8833308765),
+ Q30(0.8838834763), Q30(0.8844357310), Q30(0.8849876411), Q30(0.8855392071),
+ Q30(0.8860904300), Q30(0.8866413101), Q30(0.8871918479), Q30(0.8877420444),
+ Q30(0.8882919000), Q30(0.8888414158), Q30(0.8893905920), Q30(0.8899394292),
+ Q30(0.8904879279), Q30(0.8910360895), Q30(0.8915839135), Q30(0.8921314017),
+ Q30(0.8926785537), Q30(0.8932253704), Q30(0.8937718528), Q30(0.8943180013),
+ Q30(0.8948638164), Q30(0.8954092991), Q30(0.8959544492), Q30(0.8964992678),
+ Q30(0.8970437557), Q30(0.8975879136), Q30(0.8981317417), Q30(0.8986752401),
+ Q30(0.8992184107), Q30(0.8997612530), Q30(0.9003037680), Q30(0.9008459565),
+ Q30(0.9013878191), Q30(0.9019293557), Q30(0.9024705673), Q30(0.9030114547),
+ Q30(0.9035520186), Q30(0.9040922588), Q30(0.9046321767), Q30(0.9051717725),
+ Q30(0.9057110464), Q30(0.9062500000), Q30(0.9067886332), Q30(0.9073269465),
+ Q30(0.9078649404), Q30(0.9084026157), Q30(0.9089399735), Q30(0.9094770132),
+ Q30(0.9100137362), Q30(0.9105501426), Q30(0.9110862338), Q30(0.9116220092),
+ Q30(0.9121574699), Q30(0.9126926167), Q30(0.9132274496), Q30(0.9137619697),
+ Q30(0.9142961772), Q30(0.9148300732), Q30(0.9153636573), Q30(0.9158969307),
+ Q30(0.9164298936), Q30(0.9169625468), Q30(0.9174948912), Q30(0.9180269265),
+ Q30(0.9185586534), Q30(0.9190900731), Q30(0.9196211854), Q30(0.9201519913),
+ Q30(0.9206824913), Q30(0.9212126858), Q30(0.9217425752), Q30(0.9222721602),
+ Q30(0.9228014410), Q30(0.9233304188), Q30(0.9238590938), Q30(0.9243874662),
+ Q30(0.9249155368), Q30(0.9254433061), Q30(0.9259707746), Q30(0.9264979423),
+ Q30(0.9270248110), Q30(0.9275513799), Q30(0.9280776503), Q30(0.9286036226),
+ Q30(0.9291292969), Q30(0.9296546737), Q30(0.9301797543), Q30(0.9307045382),
+ Q30(0.9312290265), Q30(0.9317532196), Q30(0.9322771183), Q30(0.9328007223),
+ Q30(0.9333240325), Q30(0.9338470497), Q30(0.9343697741), Q30(0.9348922065),
+ Q30(0.9354143469), Q30(0.9359361958), Q30(0.9364577541), Q30(0.9369790219),
+ Q30(0.9375000000), Q30(0.9380206889), Q30(0.9385410887), Q30(0.9390612002),
+ Q30(0.9395810235), Q30(0.9401005600), Q30(0.9406198091), Q30(0.9411387718),
+ Q30(0.9416574482), Q30(0.9421758396), Q30(0.9426939455), Q30(0.9432117669),
+ Q30(0.9437293042), Q30(0.9442465580), Q30(0.9447635286), Q30(0.9452802162),
+ Q30(0.9457966220), Q30(0.9463127456), Q30(0.9468285879), Q30(0.9473441495),
+ Q30(0.9478594307), Q30(0.9483744316), Q30(0.9488891531), Q30(0.9494035956),
+ Q30(0.9499177597), Q30(0.9504316454), Q30(0.9509452535), Q30(0.9514585841),
+ Q30(0.9519716380), Q30(0.9524844158), Q30(0.9529969175), Q30(0.9535091440),
+ Q30(0.9540210953), Q30(0.9545327718), Q30(0.9550441746), Q30(0.9555553030),
+ Q30(0.9560661586), Q30(0.9565767418), Q30(0.9570870521), Q30(0.9575970904),
+ Q30(0.9581068573), Q30(0.9586163531), Q30(0.9591255784), Q30(0.9596345332),
+ Q30(0.9601432183), Q30(0.9606516343), Q30(0.9611597811), Q30(0.9616676597),
+ Q30(0.9621752701), Q30(0.9626826127), Q30(0.9631896880), Q30(0.9636964966),
+ Q30(0.9642030387), Q30(0.9647093150), Q30(0.9652153258), Q30(0.9657210712),
+ Q30(0.9662265521), Q30(0.9667317686), Q30(0.9672367214), Q30(0.9677414102),
+ Q30(0.9682458364), Q30(0.9687500000), Q30(0.9692539014), Q30(0.9697575406),
+ Q30(0.9702609186), Q30(0.9707640354), Q30(0.9712668918), Q30(0.9717694880),
+ Q30(0.9722718243), Q30(0.9727739012), Q30(0.9732757187), Q30(0.9737772783),
+ Q30(0.9742785795), Q30(0.9747796226), Q30(0.9752804083), Q30(0.9757809374),
+ Q30(0.9762812094), Q30(0.9767812253), Q30(0.9772809856), Q30(0.9777804906),
+ Q30(0.9782797401), Q30(0.9787787353), Q30(0.9792774762), Q30(0.9797759629),
+ Q30(0.9802741962), Q30(0.9807721768), Q30(0.9812699044), Q30(0.9817673797),
+ Q30(0.9822646030), Q30(0.9827615744), Q30(0.9832582953), Q30(0.9837547648),
+ Q30(0.9842509842), Q30(0.9847469535), Q30(0.9852426732), Q30(0.9857381433),
+ Q30(0.9862333648), Q30(0.9867283376), Q30(0.9872230627), Q30(0.9877175395),
+ Q30(0.9882117687), Q30(0.9887057510), Q30(0.9891994870), Q30(0.9896929762),
+ Q30(0.9901862200), Q30(0.9906792180), Q30(0.9911719705), Q30(0.9916644781),
+ Q30(0.9921567417), Q30(0.9926487608), Q30(0.9931405364), Q30(0.9936320684),
+ Q30(0.9941233573), Q30(0.9946144037), Q30(0.9951052079), Q30(0.9955957700),
+ Q30(0.9960860908), Q30(0.9965761700), Q30(0.9970660084), Q30(0.9975556061),
+ Q30(0.9980449639), Q30(0.9985340820), Q30(0.9990229602), Q30(0.9995115995),
+ 0x3FFFFFFF
+};
+
+static int sqrExpMultTab[2] = {
+ Q30(0.5000000000), Q30(0.7071067812)
+};
+
+static int aac_costbl_1[16] = {
+ Q30( 1.000000000000000), Q30( 0.980785280403230), Q30( 0.923879532511287), Q30( 0.831469612302545),
+ Q30( 0.707106781186548), Q30( 0.555570233019602), Q30( 0.382683432365090), Q30( 0.195090322016128),
+ Q30( 0.000000000000000), Q30(-0.195090322016128), Q30(-0.382683432365090), Q30(-0.555570233019602),
+ Q30(-0.707106781186547), Q30(-0.831469612302545), Q30(-0.923879532511287), Q30(-0.980785280403230)
+};
+
+static int aac_costbl_2[32] = {
+ Q30(1.000000000000000), Q30(0.999981175282601), Q30(0.999924701839145), Q30(0.999830581795823),
+ Q30(0.999698818696204), Q30(0.999529417501093), Q30(0.999322384588350), Q30(0.999077727752645),
+ Q30(0.998795456205172), Q30(0.998475580573295), Q30(0.998118112900149), Q30(0.997723066644192),
+ Q30(0.997290456678690), Q30(0.996820299291166), Q30(0.996312612182778), Q30(0.995767414467660),
+ Q30(0.995184726672197), Q30(0.994564570734255), Q30(0.993906970002356), Q30(0.993211949234795),
+ Q30(0.992479534598710), Q30(0.991709753669100), Q30(0.990902635427780), Q30(0.990058210262297),
+ Q30(0.989176509964781), Q30(0.988257567730749), Q30(0.987301418157858), Q30(0.986308097244599),
+ Q30(0.985277642388941), Q30(0.984210092386929), Q30(0.983105487431216), Q30(0.981963869109555)
+};
+
+static int aac_sintbl_2[32] = {
+ Q30(0.000000000000000), Q30(0.006135884649154), Q30(0.012271538285720), Q30(0.018406729905805),
+ Q30(0.024541228522912), Q30(0.030674803176637), Q30(0.036807222941359), Q30(0.042938256934941),
+ Q30(0.049067674327418), Q30(0.055195244349690), Q30(0.061320736302209), Q30(0.067443919563664),
+ Q30(0.073564563599667), Q30(0.079682437971430), Q30(0.085797312344440), Q30(0.091908956497133),
+ Q30(0.098017140329561), Q30(0.104121633872055), Q30(0.110222207293883), Q30(0.116318630911905),
+ Q30(0.122410675199216), Q30(0.128498110793793), Q30(0.134580708507126), Q30(0.140658239332849),
+ Q30(0.146730474455362), Q30(0.152797185258443), Q30(0.158858143333861), Q30(0.164913120489970),
+ Q30(0.170961888760301), Q30(0.177004220412149), Q30(0.183039887955141), Q30(0.189068664149806)
+};
+
+static int aac_costbl_3[32] = {
+ Q30(1.000000000000000), Q30(0.999999981616429), Q30(0.999999926465718), Q30(0.999999834547868),
+ Q30(0.999999705862882), Q30(0.999999540410766), Q30(0.999999338191526), Q30(0.999999099205168),
+ Q30(0.999998823451702), Q30(0.999998510931138), Q30(0.999998161643487), Q30(0.999997775588762),
+ Q30(0.999997352766978), Q30(0.999996893178150), Q30(0.999996396822294), Q30(0.999995863699430),
+ Q30(0.999995293809576), Q30(0.999994687152754), Q30(0.999994043728986), Q30(0.999993363538295),
+ Q30(0.999992646580707), Q30(0.999991892856248), Q30(0.999991102364946), Q30(0.999990275106829),
+ Q30(0.999989411081928), Q30(0.999988510290276), Q30(0.999987572731904), Q30(0.999986598406848),
+ Q30(0.999985587315143), Q30(0.999984539456827), Q30(0.999983454831938), Q30(0.999982333440515)
+};
+
+static int aac_sintbl_3[32] = {
+ Q30(0.000000000000000), Q30(0.000191747597311), Q30(0.000383495187571), Q30(0.000575242763732),
+ Q30(0.000766990318743), Q30(0.000958737845553), Q30(0.001150485337114), Q30(0.001342232786374),
+ Q30(0.001533980186285), Q30(0.001725727529795), Q30(0.001917474809855), Q30(0.002109222019416),
+ Q30(0.002300969151426), Q30(0.002492716198836), Q30(0.002684463154596), Q30(0.002876210011656),
+ Q30(0.003067956762966), Q30(0.003259703401476), Q30(0.003451449920136), Q30(0.003643196311896),
+ Q30(0.003834942569706), Q30(0.004026688686517), Q30(0.004218434655277), Q30(0.004410180468938),
+ Q30(0.004601926120449), Q30(0.004793671602760), Q30(0.004985416908822), Q30(0.005177162031584),
+ Q30(0.005368906963996), Q30(0.005560651699010), Q30(0.005752396229574), Q30(0.005944140548639)
+};
+
+static int aac_costbl_4[33] = {
+ Q30(1.000000000000000), Q30(0.999999999982047), Q30(0.999999999928189), Q30(0.999999999838426),
+ Q30(0.999999999712757), Q30(0.999999999551182), Q30(0.999999999353703), Q30(0.999999999120317),
+ Q30(0.999999998851027), Q30(0.999999998545831), Q30(0.999999998204729), Q30(0.999999997827723),
+ Q30(0.999999997414810), Q30(0.999999996965993), Q30(0.999999996481270), Q30(0.999999995960641),
+ Q30(0.999999995404107), Q30(0.999999994811668), Q30(0.999999994183323), Q30(0.999999993519073),
+ Q30(0.999999992818918), Q30(0.999999992082857), Q30(0.999999991310890), Q30(0.999999990503019),
+ Q30(0.999999989659241), Q30(0.999999988779559), Q30(0.999999987863971), Q30(0.999999986912477),
+ Q30(0.999999985925079), Q30(0.999999984901774), Q30(0.999999983842565), Q30(0.999999982747450),
+ Q30(0.999999981616429)
+};
+
+static int aac_sintbl_4[33] = {
+ Q30(0.000000000000000), Q30(0.000005992112453), Q30(0.000011984224905), Q30(0.000017976337357),
+ Q30(0.000023968449808), Q30(0.000029960562259), Q30(0.000035952674708), Q30(0.000041944787156),
+ Q30(0.000047936899603), Q30(0.000053929012048), Q30(0.000059921124491), Q30(0.000065913236932),
+ Q30(0.000071905349370), Q30(0.000077897461806), Q30(0.000083889574239), Q30(0.000089881686669),
+ Q30(0.000095873799096), Q30(0.000101865911519), Q30(0.000107858023939), Q30(0.000113850136355),
+ Q30(0.000119842248767), Q30(0.000125834361174), Q30(0.000131826473577), Q30(0.000137818585975),
+ Q30(0.000143810698369), Q30(0.000149802810757), Q30(0.000155794923139), Q30(0.000161787035517),
+ Q30(0.000167779147888), Q30(0.000173771260253), Q30(0.000179763372612), Q30(0.000185755484965),
+ Q30(0.000191747597311)
+};
+
+
+static av_always_inline av_const int av_log2_c_emu(unsigned int v)
+{
+ int n = 0;
+ if (v & 0xffff0000) {
+ v >>= 16;
+ n += 16;
+ }
+ if (v & 0xff00) {
+ v >>= 8;
+ n += 8;
+ }
+ n += ff_log2_tab[v];
+
+ return n;
+}
+
+static __inline aac_float_t int2float(const int x, const int exp)
+{
+ aac_float_t ret;
+ int nz;
+
+ if (x == 0)
+ {
+ ret.mant = 0;
+ ret.expo = 0;
+ }
+ else
+ {
+ ret.expo = exp;
+ ret.mant = x;
+ nz = 29 - av_log2_c_emu(FFABS(ret.mant));
+ ret.mant <<= nz;
+ ret.expo -= nz;
+ }
+
+ return ret;
+}
+
+static __inline aac_float_t float_add(aac_float_t a, aac_float_t b)
+{
+ int diff, nz;
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+ aac_float_t res;
+
+ if (manta == 0)
+ return b;
+
+ if (mantb == 0)
+ return a;
+
+ diff = expa - expb;
+ if (diff < 0) // expa < expb
+ {
+ diff = -diff;
+ if (diff >= 31)
+ manta = 0;
+ else if (diff != 0)
+ manta >>= diff;
+ expa = expb;
+ }
+ else // expa >= expb
+ {
+ if (diff >= 31)
+ mantb = 0;
+ else if (diff != 0)
+ mantb >>= diff;
+ }
+
+ manta = manta + mantb;
+ if (manta == 0)
+ expa = 0;
+ else
+ {
+ nz = 30 - av_log2_c_emu(FFABS(manta));
+ manta <<= nz;
+ manta >>= 1;
+ expa -= (nz-1);
+ }
+
+ res.mant = manta;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_sub(aac_float_t a, aac_float_t b)
+{
+ int diff, nz;
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+ aac_float_t res;
+
+ if (manta == 0)
+ {
+ res.mant = -mantb;
+ res.expo = expb;
+ return res;
+ }
+
+ if (mantb == 0)
+ return a;
+
+ diff = expa - expb;
+ if (diff < 0) // expa < expb
+ {
+ diff = -diff;
+ if (diff >= 31)
+ manta = 0;
+ else if (diff != 0)
+ manta >>= diff;
+ expa = expb;
+ }
+ else // expa >= expb
+ {
+ if (diff >= 31)
+ mantb = 0;
+ else if (diff != 0)
+ mantb >>= diff;
+ }
+
+ manta = manta - mantb;
+ if (manta == 0)
+ expa = 0;
+ else
+ {
+ nz = 30 - av_log2_c_emu(FFABS(manta));
+ manta <<= nz;
+ manta >>= 1;
+ expa -= (nz-1);
+ }
+
+ res.mant = manta;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_mul(aac_float_t a, aac_float_t b)
+{
+ aac_float_t res;
+ int mant;
+ int expa = a.expo;
+ int expb = b.expo;
+ long long accu;
+
+ expa = expa + expb;
+ accu = (long long)a.mant * b.mant;
+ mant = (int)((accu + 0x20000000) >> 30);
+ if (mant == 0)
+ expa = 0;
+ else if (mant < 536870912 && mant > -536870912)
+ {
+ mant <<= 1;
+ expa = expa - 1;
+ }
+ res.mant = mant;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_recip(const aac_float_t a)
+{
+ aac_float_t r;
+ int s;
+ int manta, expa;
+
+ manta = a.mant;
+ expa = a.expo;
+
+ expa = 1 - expa;
+ r.expo = expa;
+
+ s = manta >> 31;
+ manta = (manta ^ s) - s;
+
+ manta = divTable[(manta - 0x20000000) >> 22];
+
+ r.mant = (manta ^ s) - s;
+
+ return r;
+}
+
+static __inline aac_float_t float_div(aac_float_t a, aac_float_t b)
+{
+ aac_float_t res;
+ aac_float_t iB, tmp;
+ int mantb;
+
+ mantb = b.mant;
+ if (mantb != 0)
+ {
+ iB = float_recip(b);
+ // newton iteration to double precision
+ tmp = float_sub(FLOAT_1, float_mul(b, iB));
+ iB = float_add(iB, float_mul(iB, tmp));
+ res = float_mul(a, iB);
+ }
+ else
+ {
+ res.mant = 1;
+ res.expo = 2147483647;
+ }
+
+ return res;
+}
+
+static __inline int float_gt(aac_float_t a, aac_float_t b)
+{
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+
+ if (manta == 0)
+ expa = 0x80000000;
+
+ if (mantb == 0)
+ expb = 0x80000000;
+
+ if (expa > expb)
+ return 1;
+ else if (expa < expb)
+ return 0;
+ else // expa == expb
+ {
+ if (manta > mantb)
+ return 1;
+ else
+ return 0;
+ }
+}
+
+static __inline aac_float_t float_sqrt(aac_float_t val)
+{
+ int exp;
+ int tabIndex, rem;
+ int mant;
+ long long accu;
+ aac_float_t res;
+
+ exp = val.expo;
+ mant = val.mant;
+
+ if (mant == 0)
+ {
+ res.mant = 0;
+ res.expo = 0;
+ }
+ else
+ {
+ tabIndex = (mant - 536870912);
+ tabIndex = tabIndex >> 20;
+
+ rem = mant & 0xfffff;
+ accu = (long long)sqrtTab[tabIndex] * (0x100000-rem);
+ accu += (long long)sqrtTab[tabIndex+1] * rem;
+ mant = (int)((accu + 0x80000) >> 20);
+
+ accu = (long long)sqrExpMultTab[exp&1] * mant;
+ mant = (int)((accu + 0x10000000) >> 29);
+ if (mant < 1073741824)
+ exp -= 2;
+ else
+ mant >>= 1;
+
+ res.mant = mant;
+ res.expo = (exp>>1)+1;
+ }
+
+ return res;
+}
+
+static __inline void aac_fixed_sincos(int a, int *s, int *c)
+{
+ int idx, sign;
+ int sv, cv;
+ int st, ct;
+ long long accu;
+
+ idx = a >> 26;
+ sign = (idx << 27) >> 31;
+ cv = aac_costbl_1[idx & 0xf];
+ cv = (cv ^ sign) - sign;
+
+ idx -= 8;
+ sign = (idx << 27) >> 31;
+ sv = aac_costbl_1[idx & 0xf];
+ sv = (sv ^ sign) - sign;
+
+ idx = a >> 21;
+ ct = aac_costbl_2[idx & 0x1f];
+ st = aac_sintbl_2[idx & 0x1f];
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ idx = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ sv = (int)((accu + 0x20000000) >> 30);
+ cv = idx;
+
+ idx = a >> 16;
+ ct = aac_costbl_3[idx & 0x1f];
+ st = aac_sintbl_3[idx & 0x1f];
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ idx = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ sv = (int)((accu + 0x20000000) >> 30);
+ cv = idx;
+
+ idx = a >> 11;
+ accu = (long long)aac_costbl_4[idx & 0x1f]*(0x800 - (a&0x7ff));
+ accu += (long long)aac_costbl_4[(idx & 0x1f)+1]*(a&0x7ff);
+ ct = (int)((accu + 0x400) >> 11);
+ accu = (long long)aac_sintbl_4[idx & 0x1f]*(0x800 - (a&0x7ff));
+ accu += (long long)aac_sintbl_4[(idx & 0x1f)+1]*(a&0x7ff);
+ st = (int)((accu + 0x400) >> 11);
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ *c = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ *s = (int)((accu + 0x20000000) >> 30);
+}
+
+#undef ff_log2_tab
+#undef divTable
+#undef sqrtTab
+#undef sqrExpMultTab
+#undef aac_costbl_1
+#undef aac_costbl_2
+#undef aac_sintbl_2
+#undef aac_costbl_3
+#undef aac_sintbl_3
+#undef aac_costbl_4
+#undef aac_sintbl_4
+
+#endif
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c
index 12e09f0..fd804ec 100644
--- a/libavcodec/aacdec.c
+++ b/libavcodec/aacdec.c
@@ -31,53 +31,10 @@
* @author Maxim Gavrilov ( maxim.gavrilov gmail com )
*/
-/*
- * supported tools
- *
- * Support? Name
- * N (code in SoC repo) gain control
- * Y block switching
- * Y window shapes - standard
- * N window shapes - Low Delay
- * Y filterbank - standard
- * N (code in SoC repo) filterbank - Scalable Sample Rate
- * Y Temporal Noise Shaping
- * Y Long Term Prediction
- * Y intensity stereo
- * Y channel coupling
- * Y frequency domain prediction
- * Y Perceptual Noise Substitution
- * Y Mid/Side stereo
- * N Scalable Inverse AAC Quantization
- * N Frequency Selective Switch
- * N upsampling filter
- * Y quantization & coding - AAC
- * N quantization & coding - TwinVQ
- * N quantization & coding - BSAC
- * N AAC Error Resilience tools
- * N Error Resilience payload syntax
- * N Error Protection tool
- * N CELP
- * N Silence Compression
- * N HVXC
- * N HVXC 4kbits/s VR
- * N Structured Audio tools
- * N Structured Audio Sample Bank Format
- * N MIDI
- * N Harmonic and Individual Lines plus Noise
- * N Text-To-Speech Interface
- * Y Spectral Band Replication
- * Y (not in this code) Layer-1
- * Y (not in this code) Layer-2
- * Y (not in this code) Layer-3
- * N SinuSoidal Coding (Transient, Sinusoid, Noise)
- * Y Parametric Stereo
- * N Direct Stream Transfer
- *
- * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
- * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
- Parametric Stereo.
- */
+#define CONFIG_FFT_FLOAT 1
+#define CONFIG_FFT_FIXED_32 0
+#define CONFIG_AAC_FIXED 0
+#define CONFIG_FIXED 0
#include "libavutil/float_dsp.h"
#include "libavutil/opt.h"
@@ -111,776 +68,6 @@
# include "mips/aacdec_mips.h"
#endif
-static VLC vlc_scalefactors;
-static VLC vlc_spectral[11];
-
-static int output_configure(AACContext *ac,
- uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
- enum OCStatus oc_type, int get_new_frame);
-
-#define overread_err "Input buffer exhausted before END element found\n"
-
-static int count_channels(uint8_t (*layout)[3], int tags)
-{
- int i, sum = 0;
- for (i = 0; i < tags; i++) {
- int syn_ele = layout[i][0];
- int pos = layout[i][2];
- sum += (1 + (syn_ele == TYPE_CPE)) *
- (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
- }
- return sum;
-}
-
-/**
- * Check for the channel element in the current channel position configuration.
- * If it exists, make sure the appropriate element is allocated and map the
- * channel order to match the internal FFmpeg channel layout.
- *
- * @param che_pos current channel position configuration
- * @param type channel element type
- * @param id channel element id
- * @param channels count of the number of channels in the configuration
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static av_cold int che_configure(AACContext *ac,
- enum ChannelPosition che_pos,
- int type, int id, int *channels)
-{
- if (*channels >= MAX_CHANNELS)
- return AVERROR_INVALIDDATA;
- if (che_pos) {
- if (!ac->che[type][id]) {
- if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
- return AVERROR(ENOMEM);
- ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
- }
- if (type != TYPE_CCE) {
- if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
- av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
- return AVERROR_INVALIDDATA;
- }
- ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
- if (type == TYPE_CPE ||
- (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
- ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
- }
- }
- } else {
- if (ac->che[type][id])
- ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
- av_freep(&ac->che[type][id]);
- }
- return 0;
-}
-
-static int frame_configure_elements(AVCodecContext *avctx)
-{
- AACContext *ac = avctx->priv_data;
- int type, id, ch, ret;
-
- /* set channel pointers to internal buffers by default */
- for (type = 0; type < 4; type++) {
- for (id = 0; id < MAX_ELEM_ID; id++) {
- ChannelElement *che = ac->che[type][id];
- if (che) {
- che->ch[0].ret = che->ch[0].ret_buf;
- che->ch[1].ret = che->ch[1].ret_buf;
- }
- }
- }
-
- /* get output buffer */
- av_frame_unref(ac->frame);
- ac->frame->nb_samples = 2048;
- if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
- return ret;
-
- /* map output channel pointers to AVFrame data */
- for (ch = 0; ch < avctx->channels; ch++) {
- if (ac->output_element[ch])
- ac->output_element[ch]->ret = (float *)ac->frame->extended_data[ch];
- }
-
- return 0;
-}
-
-struct elem_to_channel {
- uint64_t av_position;
- uint8_t syn_ele;
- uint8_t elem_id;
- uint8_t aac_position;
-};
-
-static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
- uint8_t (*layout_map)[3], int offset, uint64_t left,
- uint64_t right, int pos)
-{
- if (layout_map[offset][0] == TYPE_CPE) {
- e2c_vec[offset] = (struct elem_to_channel) {
- .av_position = left | right,
- .syn_ele = TYPE_CPE,
- .elem_id = layout_map[offset][1],
- .aac_position = pos
- };
- return 1;
- } else {
- e2c_vec[offset] = (struct elem_to_channel) {
- .av_position = left,
- .syn_ele = TYPE_SCE,
- .elem_id = layout_map[offset][1],
- .aac_position = pos
- };
- e2c_vec[offset + 1] = (struct elem_to_channel) {
- .av_position = right,
- .syn_ele = TYPE_SCE,
- .elem_id = layout_map[offset + 1][1],
- .aac_position = pos
- };
- return 2;
- }
-}
-
-static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
- int *current)
-{
- int num_pos_channels = 0;
- int first_cpe = 0;
- int sce_parity = 0;
- int i;
- for (i = *current; i < tags; i++) {
- if (layout_map[i][2] != pos)
- break;
- if (layout_map[i][0] == TYPE_CPE) {
- if (sce_parity) {
- if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
- sce_parity = 0;
- } else {
- return -1;
- }
- }
- num_pos_channels += 2;
- first_cpe = 1;
- } else {
- num_pos_channels++;
- sce_parity ^= 1;
- }
- }
- if (sce_parity &&
- ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
- return -1;
- *current = i;
- return num_pos_channels;
-}
-
-static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
-{
- int i, n, total_non_cc_elements;
- struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
- int num_front_channels, num_side_channels, num_back_channels;
- uint64_t layout;
-
- if (FF_ARRAY_ELEMS(e2c_vec) < tags)
- return 0;
-
- i = 0;
- num_front_channels =
- count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
- if (num_front_channels < 0)
- return 0;
- num_side_channels =
- count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
- if (num_side_channels < 0)
- return 0;
- num_back_channels =
- count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
- if (num_back_channels < 0)
- return 0;
-
- i = 0;
- if (num_front_channels & 1) {
- e2c_vec[i] = (struct elem_to_channel) {
- .av_position = AV_CH_FRONT_CENTER,
- .syn_ele = TYPE_SCE,
- .elem_id = layout_map[i][1],
- .aac_position = AAC_CHANNEL_FRONT
- };
- i++;
- num_front_channels--;
- }
- if (num_front_channels >= 4) {
- i += assign_pair(e2c_vec, layout_map, i,
- AV_CH_FRONT_LEFT_OF_CENTER,
- AV_CH_FRONT_RIGHT_OF_CENTER,
- AAC_CHANNEL_FRONT);
- num_front_channels -= 2;
- }
- if (num_front_channels >= 2) {
- i += assign_pair(e2c_vec, layout_map, i,
- AV_CH_FRONT_LEFT,
- AV_CH_FRONT_RIGHT,
- AAC_CHANNEL_FRONT);
- num_front_channels -= 2;
- }
- while (num_front_channels >= 2) {
- i += assign_pair(e2c_vec, layout_map, i,
- UINT64_MAX,
- UINT64_MAX,
- AAC_CHANNEL_FRONT);
- num_front_channels -= 2;
- }
-
- if (num_side_channels >= 2) {
- i += assign_pair(e2c_vec, layout_map, i,
- AV_CH_SIDE_LEFT,
- AV_CH_SIDE_RIGHT,
- AAC_CHANNEL_FRONT);
- num_side_channels -= 2;
- }
- while (num_side_channels >= 2) {
- i += assign_pair(e2c_vec, layout_map, i,
- UINT64_MAX,
- UINT64_MAX,
- AAC_CHANNEL_SIDE);
- num_side_channels -= 2;
- }
-
- while (num_back_channels >= 4) {
- i += assign_pair(e2c_vec, layout_map, i,
- UINT64_MAX,
- UINT64_MAX,
- AAC_CHANNEL_BACK);
- num_back_channels -= 2;
- }
- if (num_back_channels >= 2) {
- i += assign_pair(e2c_vec, layout_map, i,
- AV_CH_BACK_LEFT,
- AV_CH_BACK_RIGHT,
- AAC_CHANNEL_BACK);
- num_back_channels -= 2;
- }
- if (num_back_channels) {
- e2c_vec[i] = (struct elem_to_channel) {
- .av_position = AV_CH_BACK_CENTER,
- .syn_ele = TYPE_SCE,
- .elem_id = layout_map[i][1],
- .aac_position = AAC_CHANNEL_BACK
- };
- i++;
- num_back_channels--;
- }
-
- if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
- e2c_vec[i] = (struct elem_to_channel) {
- .av_position = AV_CH_LOW_FREQUENCY,
- .syn_ele = TYPE_LFE,
- .elem_id = layout_map[i][1],
- .aac_position = AAC_CHANNEL_LFE
- };
- i++;
- }
- while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
- e2c_vec[i] = (struct elem_to_channel) {
- .av_position = UINT64_MAX,
- .syn_ele = TYPE_LFE,
- .elem_id = layout_map[i][1],
- .aac_position = AAC_CHANNEL_LFE
- };
- i++;
- }
-
- // Must choose a stable sort
- total_non_cc_elements = n = i;
- do {
- int next_n = 0;
- for (i = 1; i < n; i++)
- if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
- FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
- next_n = i;
- }
- n = next_n;
- } while (n > 0);
-
- layout = 0;
- for (i = 0; i < total_non_cc_elements; i++) {
- layout_map[i][0] = e2c_vec[i].syn_ele;
- layout_map[i][1] = e2c_vec[i].elem_id;
- layout_map[i][2] = e2c_vec[i].aac_position;
- if (e2c_vec[i].av_position != UINT64_MAX) {
- layout |= e2c_vec[i].av_position;
- }
- }
-
- return layout;
-}
-
-/**
- * Save current output configuration if and only if it has been locked.
- */
-static void push_output_configuration(AACContext *ac) {
- if (ac->oc[1].status == OC_LOCKED) {
- ac->oc[0] = ac->oc[1];
- }
- ac->oc[1].status = OC_NONE;
-}
-
-/**
- * Restore the previous output configuration if and only if the current
- * configuration is unlocked.
- */
-static void pop_output_configuration(AACContext *ac) {
- if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
- ac->oc[1] = ac->oc[0];
- ac->avctx->channels = ac->oc[1].channels;
- ac->avctx->channel_layout = ac->oc[1].channel_layout;
- output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
- ac->oc[1].status, 0);
- }
-}
-
-/**
- * Configure output channel order based on the current program
- * configuration element.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int output_configure(AACContext *ac,
- uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
- enum OCStatus oc_type, int get_new_frame)
-{
- AVCodecContext *avctx = ac->avctx;
- int i, channels = 0, ret;
- uint64_t layout = 0;
-
- if (ac->oc[1].layout_map != layout_map) {
- memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
- ac->oc[1].layout_map_tags = tags;
- }
-
- // Try to sniff a reasonable channel order, otherwise output the
- // channels in the order the PCE declared them.
- if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
- layout = sniff_channel_order(layout_map, tags);
- for (i = 0; i < tags; i++) {
- int type = layout_map[i][0];
- int id = layout_map[i][1];
- int position = layout_map[i][2];
- // Allocate or free elements depending on if they are in the
- // current program configuration.
- ret = che_configure(ac, position, type, id, &channels);
- if (ret < 0)
- return ret;
- }
- if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
- if (layout == AV_CH_FRONT_CENTER) {
- layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
- } else {
- layout = 0;
- }
- }
-
- memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
- if (layout) avctx->channel_layout = layout;
- ac->oc[1].channel_layout = layout;
- avctx->channels = ac->oc[1].channels = channels;
- ac->oc[1].status = oc_type;
-
- if (get_new_frame) {
- if ((ret = frame_configure_elements(ac->avctx)) < 0)
- return ret;
- }
-
- return 0;
-}
-
-static void flush(AVCodecContext *avctx)
-{
- AACContext *ac= avctx->priv_data;
- int type, i, j;
-
- for (type = 3; type >= 0; type--) {
- for (i = 0; i < MAX_ELEM_ID; i++) {
- ChannelElement *che = ac->che[type][i];
- if (che) {
- for (j = 0; j <= 1; j++) {
- memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
- }
- }
- }
- }
-}
-
-/**
- * Set up channel positions based on a default channel configuration
- * as specified in table 1.17.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int set_default_channel_config(AVCodecContext *avctx,
- uint8_t (*layout_map)[3],
- int *tags,
- int channel_config)
-{
- if (channel_config < 1 || channel_config > 7) {
- av_log(avctx, AV_LOG_ERROR,
- "invalid default channel configuration (%d)\n",
- channel_config);
- return AVERROR_INVALIDDATA;
- }
- *tags = tags_per_config[channel_config];
- memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
- *tags * sizeof(*layout_map));
- return 0;
-}
-
-static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
-{
- /* For PCE based channel configurations map the channels solely based
- * on tags. */
- if (!ac->oc[1].m4ac.chan_config) {
- return ac->tag_che_map[type][elem_id];
- }
- // Allow single CPE stereo files to be signalled with mono configuration.
- if (!ac->tags_mapped && type == TYPE_CPE &&
- ac->oc[1].m4ac.chan_config == 1) {
- uint8_t layout_map[MAX_ELEM_ID*4][3];
- int layout_map_tags;
- push_output_configuration(ac);
-
- av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
-
- if (set_default_channel_config(ac->avctx, layout_map,
- &layout_map_tags, 2) < 0)
- return NULL;
- if (output_configure(ac, layout_map, layout_map_tags,
- OC_TRIAL_FRAME, 1) < 0)
- return NULL;
-
- ac->oc[1].m4ac.chan_config = 2;
- ac->oc[1].m4ac.ps = 0;
- }
- // And vice-versa
- if (!ac->tags_mapped && type == TYPE_SCE &&
- ac->oc[1].m4ac.chan_config == 2) {
- uint8_t layout_map[MAX_ELEM_ID * 4][3];
- int layout_map_tags;
- push_output_configuration(ac);
-
- av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
-
- if (set_default_channel_config(ac->avctx, layout_map,
- &layout_map_tags, 1) < 0)
- return NULL;
- if (output_configure(ac, layout_map, layout_map_tags,
- OC_TRIAL_FRAME, 1) < 0)
- return NULL;
-
- ac->oc[1].m4ac.chan_config = 1;
- if (ac->oc[1].m4ac.sbr)
- ac->oc[1].m4ac.ps = -1;
- }
- /* For indexed channel configurations map the channels solely based
- * on position. */
- switch (ac->oc[1].m4ac.chan_config) {
- case 7:
- if (ac->tags_mapped == 3 && type == TYPE_CPE) {
- ac->tags_mapped++;
- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
- }
- case 6:
- /* Some streams incorrectly code 5.1 audio as
- * SCE[0] CPE[0] CPE[1] SCE[1]
- * instead of
- * SCE[0] CPE[0] CPE[1] LFE[0].
- * If we seem to have encountered such a stream, transfer
- * the LFE[0] element to the SCE[1]'s mapping */
- if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
- ac->tags_mapped++;
- return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
- }
- case 5:
- if (ac->tags_mapped == 2 && type == TYPE_CPE) {
- ac->tags_mapped++;
- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
- }
- case 4:
- if (ac->tags_mapped == 2 &&
- ac->oc[1].m4ac.chan_config == 4 &&
- type == TYPE_SCE) {
- ac->tags_mapped++;
- return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
- }
- case 3:
- case 2:
- if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
- type == TYPE_CPE) {
- ac->tags_mapped++;
- return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
- } else if (ac->oc[1].m4ac.chan_config == 2) {
- return NULL;
- }
- case 1:
- if (!ac->tags_mapped && type == TYPE_SCE) {
- ac->tags_mapped++;
- return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
- }
- default:
- return NULL;
- }
-}
-
-/**
- * Decode an array of 4 bit element IDs, optionally interleaved with a
- * stereo/mono switching bit.
- *
- * @param type speaker type/position for these channels
- */
-static void decode_channel_map(uint8_t layout_map[][3],
- enum ChannelPosition type,
- GetBitContext *gb, int n)
-{
- while (n--) {
- enum RawDataBlockType syn_ele;
- switch (type) {
- case AAC_CHANNEL_FRONT:
- case AAC_CHANNEL_BACK:
- case AAC_CHANNEL_SIDE:
- syn_ele = get_bits1(gb);
- break;
- case AAC_CHANNEL_CC:
- skip_bits1(gb);
- syn_ele = TYPE_CCE;
- break;
- case AAC_CHANNEL_LFE:
- syn_ele = TYPE_LFE;
- break;
- default:
- av_assert0(0);
- }
- layout_map[0][0] = syn_ele;
- layout_map[0][1] = get_bits(gb, 4);
- layout_map[0][2] = type;
- layout_map++;
- }
-}
-
-/**
- * Decode program configuration element; reference: table 4.2.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
- uint8_t (*layout_map)[3],
- GetBitContext *gb)
-{
- int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
- int sampling_index;
- int comment_len;
- int tags;
-
- skip_bits(gb, 2); // object_type
-
- sampling_index = get_bits(gb, 4);
- if (m4ac->sampling_index != sampling_index)
- av_log(avctx, AV_LOG_WARNING,
- "Sample rate index in program config element does not "
- "match the sample rate index configured by the container.\n");
-
- num_front = get_bits(gb, 4);
- num_side = get_bits(gb, 4);
- num_back = get_bits(gb, 4);
- num_lfe = get_bits(gb, 2);
- num_assoc_data = get_bits(gb, 3);
- num_cc = get_bits(gb, 4);
-
- if (get_bits1(gb))
- skip_bits(gb, 4); // mono_mixdown_tag
- if (get_bits1(gb))
- skip_bits(gb, 4); // stereo_mixdown_tag
-
- if (get_bits1(gb))
- skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
-
- if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
- av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
- return -1;
- }
- decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
- tags = num_front;
- decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
- tags += num_side;
- decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
- tags += num_back;
- decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
- tags += num_lfe;
-
- skip_bits_long(gb, 4 * num_assoc_data);
-
- decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
- tags += num_cc;
-
- align_get_bits(gb);
-
- /* comment field, first byte is length */
- comment_len = get_bits(gb, 8) * 8;
- if (get_bits_left(gb) < comment_len) {
- av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
- return AVERROR_INVALIDDATA;
- }
- skip_bits_long(gb, comment_len);
- return tags;
-}
-
-/**
- * Decode GA "General Audio" specific configuration; reference: table 4.1.
- *
- * @param ac pointer to AACContext, may be null
- * @param avctx pointer to AVCCodecContext, used for logging
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
- GetBitContext *gb,
- MPEG4AudioConfig *m4ac,
- int channel_config)
-{
- int extension_flag, ret;
- uint8_t layout_map[MAX_ELEM_ID*4][3];
- int tags = 0;
-
- if (get_bits1(gb)) { // frameLengthFlag
- avpriv_request_sample(avctx, "960/120 MDCT window");
- return AVERROR_PATCHWELCOME;
- }
-
- if (get_bits1(gb)) // dependsOnCoreCoder
- skip_bits(gb, 14); // coreCoderDelay
- extension_flag = get_bits1(gb);
-
- if (m4ac->object_type == AOT_AAC_SCALABLE ||
- m4ac->object_type == AOT_ER_AAC_SCALABLE)
- skip_bits(gb, 3); // layerNr
-
- if (channel_config == 0) {
- skip_bits(gb, 4); // element_instance_tag
- tags = decode_pce(avctx, m4ac, layout_map, gb);
- if (tags < 0)
- return tags;
- } else {
- if ((ret = set_default_channel_config(avctx, layout_map,
- &tags, channel_config)))
- return ret;
- }
-
- if (count_channels(layout_map, tags) > 1) {
- m4ac->ps = 0;
- } else if (m4ac->sbr == 1 && m4ac->ps == -1)
- m4ac->ps = 1;
-
- if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
- return ret;
-
- if (extension_flag) {
- switch (m4ac->object_type) {
- case AOT_ER_BSAC:
- skip_bits(gb, 5); // numOfSubFrame
- skip_bits(gb, 11); // layer_length
- break;
- case AOT_ER_AAC_LC:
- case AOT_ER_AAC_LTP:
- case AOT_ER_AAC_SCALABLE:
- case AOT_ER_AAC_LD:
- skip_bits(gb, 3); /* aacSectionDataResilienceFlag
- * aacScalefactorDataResilienceFlag
- * aacSpectralDataResilienceFlag
- */
- break;
- }
- skip_bits1(gb); // extensionFlag3 (TBD in version 3)
- }
- return 0;
-}
-
-/**
- * Decode audio specific configuration; reference: table 1.13.
- *
- * @param ac pointer to AACContext, may be null
- * @param avctx pointer to AVCCodecContext, used for logging
- * @param m4ac pointer to MPEG4AudioConfig, used for parsing
- * @param data pointer to buffer holding an audio specific config
- * @param bit_size size of audio specific config or data in bits
- * @param sync_extension look for an appended sync extension
- *
- * @return Returns error status or number of consumed bits. <0 - error
- */
-static int decode_audio_specific_config(AACContext *ac,
- AVCodecContext *avctx,
- MPEG4AudioConfig *m4ac,
- const uint8_t *data, int bit_size,
- int sync_extension)
-{
- GetBitContext gb;
- int i, ret;
-
- av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
- for (i = 0; i < bit_size >> 3; i++)
- av_dlog(avctx, "%02x ", data[i]);
- av_dlog(avctx, "\n");
-
- if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
- return ret;
-
- if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
- sync_extension)) < 0)
- return AVERROR_INVALIDDATA;
- if (m4ac->sampling_index > 12) {
- av_log(avctx, AV_LOG_ERROR,
- "invalid sampling rate index %d\n",
- m4ac->sampling_index);
- return AVERROR_INVALIDDATA;
- }
-
- skip_bits_long(&gb, i);
-
- switch (m4ac->object_type) {
- case AOT_AAC_MAIN:
- case AOT_AAC_LC:
- case AOT_AAC_LTP:
- if ((ret = decode_ga_specific_config(ac, avctx, &gb,
- m4ac, m4ac->chan_config)) < 0)
- return ret;
- break;
- default:
- av_log(avctx, AV_LOG_ERROR,
- "Audio object type %s%d is not supported.\n",
- m4ac->sbr == 1 ? "SBR+" : "",
- m4ac->object_type);
- return AVERROR(ENOSYS);
- }
-
- av_dlog(avctx,
- "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
- m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
- m4ac->sample_rate, m4ac->sbr,
- m4ac->ps);
-
- return get_bits_count(&gb);
-}
-
-/**
- * linear congruential pseudorandom number generator
- *
- * @param previous_val pointer to the current state of the generator
- *
- * @return Returns a 32-bit pseudorandom integer
- */
-static av_always_inline int lcg_random(unsigned previous_val)
-{
- union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
- return v.s;
-}
-
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0 = 0.0f;
@@ -891,1480 +78,136 @@ static av_always_inline void reset_predict_state(PredictorState *ps)
ps->var1 = 1.0f;
}
-static void reset_all_predictors(PredictorState *ps)
-{
- int i;
- for (i = 0; i < MAX_PREDICTORS; i++)
- reset_predict_state(&ps[i]);
-}
-
-static int sample_rate_idx (int rate)
-{
- if (92017 <= rate) return 0;
- else if (75132 <= rate) return 1;
- else if (55426 <= rate) return 2;
- else if (46009 <= rate) return 3;
- else if (37566 <= rate) return 4;
- else if (27713 <= rate) return 5;
- else if (23004 <= rate) return 6;
- else if (18783 <= rate) return 7;
- else if (13856 <= rate) return 8;
- else if (11502 <= rate) return 9;
- else if (9391 <= rate) return 10;
- else return 11;
-}
-
-static void reset_predictor_group(PredictorState *ps, int group_num)
-{
- int i;
- for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
- reset_predict_state(&ps[i]);
-}
-
-#define AAC_INIT_VLC_STATIC(num, size) \
- INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
- ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
- sizeof(ff_aac_spectral_bits[num][0]), \
- ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
- sizeof(ff_aac_spectral_codes[num][0]), \
- size);
-
-static void aacdec_init(AACContext *ac);
-
-static av_cold int aac_decode_init(AVCodecContext *avctx)
-{
- AACContext *ac = avctx->priv_data;
- int ret;
-
- ac->avctx = avctx;
- ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
-
- aacdec_init(ac);
-
- avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
-
- if (avctx->extradata_size > 0) {
- if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
- avctx->extradata,
- avctx->extradata_size * 8,
- 1)) < 0)
- return ret;
- } else {
- int sr, i;
- uint8_t layout_map[MAX_ELEM_ID*4][3];
- int layout_map_tags;
-
- sr = sample_rate_idx(avctx->sample_rate);
- ac->oc[1].m4ac.sampling_index = sr;
- ac->oc[1].m4ac.channels = avctx->channels;
- ac->oc[1].m4ac.sbr = -1;
- ac->oc[1].m4ac.ps = -1;
-
- for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
- if (ff_mpeg4audio_channels[i] == avctx->channels)
- break;
- if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
- i = 0;
- }
- ac->oc[1].m4ac.chan_config = i;
-
- if (ac->oc[1].m4ac.chan_config) {
- int ret = set_default_channel_config(avctx, layout_map,
- &layout_map_tags, ac->oc[1].m4ac.chan_config);
- if (!ret)
- output_configure(ac, layout_map, layout_map_tags,
- OC_GLOBAL_HDR, 0);
- else if (avctx->err_recognition & AV_EF_EXPLODE)
- return AVERROR_INVALIDDATA;
- }
- }
-
- if (avctx->channels > MAX_CHANNELS) {
- av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
- return AVERROR_INVALIDDATA;
- }
-
- AAC_INIT_VLC_STATIC( 0, 304);
- AAC_INIT_VLC_STATIC( 1, 270);
- AAC_INIT_VLC_STATIC( 2, 550);
- AAC_INIT_VLC_STATIC( 3, 300);
- AAC_INIT_VLC_STATIC( 4, 328);
- AAC_INIT_VLC_STATIC( 5, 294);
- AAC_INIT_VLC_STATIC( 6, 306);
- AAC_INIT_VLC_STATIC( 7, 268);
- AAC_INIT_VLC_STATIC( 8, 510);
- AAC_INIT_VLC_STATIC( 9, 366);
- AAC_INIT_VLC_STATIC(10, 462);
-
- ff_aac_sbr_init();
-
- ff_fmt_convert_init(&ac->fmt_conv, avctx);
- avpriv_float_dsp_init(&ac->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
-
- ac->random_state = 0x1f2e3d4c;
-
- ff_aac_tableinit();
-
- INIT_VLC_STATIC(&vlc_scalefactors, 7,
- FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
- ff_aac_scalefactor_bits,
- sizeof(ff_aac_scalefactor_bits[0]),
- sizeof(ff_aac_scalefactor_bits[0]),
- ff_aac_scalefactor_code,
- sizeof(ff_aac_scalefactor_code[0]),
- sizeof(ff_aac_scalefactor_code[0]),
- 352);
-
- ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
- ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
- ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
- // window initialization
- ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
- ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
- ff_init_ff_sine_windows(10);
- ff_init_ff_sine_windows( 7);
-
- cbrt_tableinit();
-
- return 0;
-}
-
-/**
- * Skip data_stream_element; reference: table 4.10.
- */
-static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
-{
- int byte_align = get_bits1(gb);
- int count = get_bits(gb, 8);
- if (count == 255)
- count += get_bits(gb, 8);
- if (byte_align)
- align_get_bits(gb);
-
- if (get_bits_left(gb) < 8 * count) {
- av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
- return AVERROR_INVALIDDATA;
- }
- skip_bits_long(gb, 8 * count);
- return 0;
-}
-
-static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
- GetBitContext *gb)
-{
- int sfb;
- if (get_bits1(gb)) {
- ics->predictor_reset_group = get_bits(gb, 5);
- if (ics->predictor_reset_group == 0 ||
- ics->predictor_reset_group > 30) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Invalid Predictor Reset Group.\n");
- return AVERROR_INVALIDDATA;
- }
- }
- for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
- ics->prediction_used[sfb] = get_bits1(gb);
- }
- return 0;
-}
-
-/**
- * Decode Long Term Prediction data; reference: table 4.xx.
- */
-static void decode_ltp(LongTermPrediction *ltp,
- GetBitContext *gb, uint8_t max_sfb)
+#ifndef VMUL2
+static inline float *VMUL2(float *dst, const float *v, unsigned idx,
+ const float *scale)
{
- int sfb;
-
- ltp->lag = get_bits(gb, 11);
- ltp->coef = ltp_coef[get_bits(gb, 3)];
- for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
- ltp->used[sfb] = get_bits1(gb);
+ float s = *scale;
+ *dst++ = v[idx & 15] * s;
+ *dst++ = v[idx>>4 & 15] * s;
+ return dst;
}
-
-/**
- * Decode Individual Channel Stream info; reference: table 4.6.
- */
-static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
- GetBitContext *gb)
-{
- if (get_bits1(gb)) {
- av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
- return AVERROR_INVALIDDATA;
- }
- ics->window_sequence[1] = ics->window_sequence[0];
- ics->window_sequence[0] = get_bits(gb, 2);
- ics->use_kb_window[1] = ics->use_kb_window[0];
- ics->use_kb_window[0] = get_bits1(gb);
- ics->num_window_groups = 1;
- ics->group_len[0] = 1;
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- int i;
- ics->max_sfb = get_bits(gb, 4);
- for (i = 0; i < 7; i++) {
- if (get_bits1(gb)) {
- ics->group_len[ics->num_window_groups - 1]++;
- } else {
- ics->num_window_groups++;
- ics->group_len[ics->num_window_groups - 1] = 1;
- }
- }
- ics->num_windows = 8;
- ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
- ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
- ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
- ics->predictor_present = 0;
- } else {
- ics->max_sfb = get_bits(gb, 6);
- ics->num_windows = 1;
- ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
- ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
- ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
- ics->predictor_present = get_bits1(gb);
- ics->predictor_reset_group = 0;
- if (ics->predictor_present) {
- if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
- if (decode_prediction(ac, ics, gb)) {
- goto fail;
- }
- } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Prediction is not allowed in AAC-LC.\n");
- goto fail;
- } else {
- if ((ics->ltp.present = get_bits(gb, 1)))
- decode_ltp(&ics->ltp, gb, ics->max_sfb);
- }
- }
- }
-
- if (ics->max_sfb > ics->num_swb) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Number of scalefactor bands in group (%d) "
- "exceeds limit (%d).\n",
- ics->max_sfb, ics->num_swb);
- goto fail;
- }
-
- return 0;
-fail:
- ics->max_sfb = 0;
- return AVERROR_INVALIDDATA;
-}
-
-/**
- * Decode band types (section_data payload); reference: table 4.46.
- *
- * @param band_type array of the used band type
- * @param band_type_run_end array of the last scalefactor band of a band type run
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_band_types(AACContext *ac, enum BandType band_type[120],
- int band_type_run_end[120], GetBitContext *gb,
- IndividualChannelStream *ics)
-{
- int g, idx = 0;
- const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
- for (g = 0; g < ics->num_window_groups; g++) {
- int k = 0;
- while (k < ics->max_sfb) {
- uint8_t sect_end = k;
- int sect_len_incr;
- int sect_band_type = get_bits(gb, 4);
- if (sect_band_type == 12) {
- av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
- return AVERROR_INVALIDDATA;
- }
- do {
- sect_len_incr = get_bits(gb, bits);
- sect_end += sect_len_incr;
- if (get_bits_left(gb) < 0) {
- av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
- return AVERROR_INVALIDDATA;
- }
- if (sect_end > ics->max_sfb) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Number of bands (%d) exceeds limit (%d).\n",
- sect_end, ics->max_sfb);
- return AVERROR_INVALIDDATA;
- }
- } while (sect_len_incr == (1 << bits) - 1);
- for (; k < sect_end; k++) {
- band_type [idx] = sect_band_type;
- band_type_run_end[idx++] = sect_end;
- }
- }
- }
- return 0;
-}
-
-/**
- * Decode scalefactors; reference: table 4.47.
- *
- * @param global_gain first scalefactor value as scalefactors are differentially coded
- * @param band_type array of the used band type
- * @param band_type_run_end array of the last scalefactor band of a band type run
- * @param sf array of scalefactors or intensity stereo positions
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
- unsigned int global_gain,
- IndividualChannelStream *ics,
- enum BandType band_type[120],
- int band_type_run_end[120])
-{
- int g, i, idx = 0;
- int offset[3] = { global_gain, global_gain - 90, 0 };
- int clipped_offset;
- int noise_flag = 1;
- for (g = 0; g < ics->num_window_groups; g++) {
- for (i = 0; i < ics->max_sfb;) {
- int run_end = band_type_run_end[idx];
- if (band_type[idx] == ZERO_BT) {
- for (; i < run_end; i++, idx++)
- sf[idx] = 0.0;
- } else if ((band_type[idx] == INTENSITY_BT) ||
- (band_type[idx] == INTENSITY_BT2)) {
- for (; i < run_end; i++, idx++) {
- offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
- clipped_offset = av_clip(offset[2], -155, 100);
- if (offset[2] != clipped_offset) {
- avpriv_request_sample(ac->avctx,
- "If you heard an audible artifact, there may be a bug in the decoder. "
- "Clipped intensity stereo position (%d -> %d)",
- offset[2], clipped_offset);
- }
- sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
- }
- } else if (band_type[idx] == NOISE_BT) {
- for (; i < run_end; i++, idx++) {
- if (noise_flag-- > 0)
- offset[1] += get_bits(gb, 9) - 256;
- else
- offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
- clipped_offset = av_clip(offset[1], -100, 155);
- if (offset[1] != clipped_offset) {
- avpriv_request_sample(ac->avctx,
- "If you heard an audible artifact, there may be a bug in the decoder. "
- "Clipped noise gain (%d -> %d)",
- offset[1], clipped_offset);
- }
- sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
- }
- } else {
- for (; i < run_end; i++, idx++) {
- offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
- if (offset[0] > 255U) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Scalefactor (%d) out of range.\n", offset[0]);
- return AVERROR_INVALIDDATA;
- }
- sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
- }
- }
- }
- }
- return 0;
-}
-
-/**
- * Decode pulse data; reference: table 4.7.
- */
-static int decode_pulses(Pulse *pulse, GetBitContext *gb,
- const uint16_t *swb_offset, int num_swb)
-{
- int i, pulse_swb;
- pulse->num_pulse = get_bits(gb, 2) + 1;
- pulse_swb = get_bits(gb, 6);
- if (pulse_swb >= num_swb)
- return -1;
- pulse->pos[0] = swb_offset[pulse_swb];
- pulse->pos[0] += get_bits(gb, 5);
- if (pulse->pos[0] > 1023)
- return -1;
- pulse->amp[0] = get_bits(gb, 4);
- for (i = 1; i < pulse->num_pulse; i++) {
- pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
- if (pulse->pos[i] > 1023)
- return -1;
- pulse->amp[i] = get_bits(gb, 4);
- }
- return 0;
-}
-
-/**
- * Decode Temporal Noise Shaping data; reference: table 4.48.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
- GetBitContext *gb, const IndividualChannelStream *ics)
-{
- int w, filt, i, coef_len, coef_res, coef_compress;
- const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
- const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
- for (w = 0; w < ics->num_windows; w++) {
- if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
- coef_res = get_bits1(gb);
-
- for (filt = 0; filt < tns->n_filt[w]; filt++) {
- int tmp2_idx;
- tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
-
- if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "TNS filter order %d is greater than maximum %d.\n",
- tns->order[w][filt], tns_max_order);
- tns->order[w][filt] = 0;
- return AVERROR_INVALIDDATA;
- }
- if (tns->order[w][filt]) {
- tns->direction[w][filt] = get_bits1(gb);
- coef_compress = get_bits1(gb);
- coef_len = coef_res + 3 - coef_compress;
- tmp2_idx = 2 * coef_compress + coef_res;
-
- for (i = 0; i < tns->order[w][filt]; i++)
- tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
- }
- }
- }
- }
- return 0;
-}
-
-/**
- * Decode Mid/Side data; reference: table 4.54.
- *
- * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
- * [1] mask is decoded from bitstream; [2] mask is all 1s;
- * [3] reserved for scalable AAC
- */
-static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
- int ms_present)
-{
- int idx;
- if (ms_present == 1) {
- for (idx = 0;
- idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
- idx++)
- cpe->ms_mask[idx] = get_bits1(gb);
- } else if (ms_present == 2) {
- memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
- }
-}
-
-#ifndef VMUL2
-static inline float *VMUL2(float *dst, const float *v, unsigned idx,
- const float *scale)
-{
- float s = *scale;
- *dst++ = v[idx & 15] * s;
- *dst++ = v[idx>>4 & 15] * s;
- return dst;
-}
-#endif
+#endif
#ifndef VMUL4
static inline float *VMUL4(float *dst, const float *v, unsigned idx,
const float *scale)
-{
- float s = *scale;
- *dst++ = v[idx & 3] * s;
- *dst++ = v[idx>>2 & 3] * s;
- *dst++ = v[idx>>4 & 3] * s;
- *dst++ = v[idx>>6 & 3] * s;
- return dst;
-}
-#endif
-
-#ifndef VMUL2S
-static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
- unsigned sign, const float *scale)
-{
- union av_intfloat32 s0, s1;
-
- s0.f = s1.f = *scale;
- s0.i ^= sign >> 1 << 31;
- s1.i ^= sign << 31;
-
- *dst++ = v[idx & 15] * s0.f;
- *dst++ = v[idx>>4 & 15] * s1.f;
-
- return dst;
-}
-#endif
-
-#ifndef VMUL4S
-static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
- unsigned sign, const float *scale)
-{
- unsigned nz = idx >> 12;
- union av_intfloat32 s = { .f = *scale };
- union av_intfloat32 t;
-
- t.i = s.i ^ (sign & 1U<<31);
- *dst++ = v[idx & 3] * t.f;
-
- sign <<= nz & 1; nz >>= 1;
- t.i = s.i ^ (sign & 1U<<31);
- *dst++ = v[idx>>2 & 3] * t.f;
-
- sign <<= nz & 1; nz >>= 1;
- t.i = s.i ^ (sign & 1U<<31);
- *dst++ = v[idx>>4 & 3] * t.f;
-
- sign <<= nz & 1;
- t.i = s.i ^ (sign & 1U<<31);
- *dst++ = v[idx>>6 & 3] * t.f;
-
- return dst;
-}
-#endif
-
-/**
- * Decode spectral data; reference: table 4.50.
- * Dequantize and scale spectral data; reference: 4.6.3.3.
- *
- * @param coef array of dequantized, scaled spectral data
- * @param sf array of scalefactors or intensity stereo positions
- * @param pulse_present set if pulses are present
- * @param pulse pointer to pulse data struct
- * @param band_type array of the used band type
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
- GetBitContext *gb, const float sf[120],
- int pulse_present, const Pulse *pulse,
- const IndividualChannelStream *ics,
- enum BandType band_type[120])
-{
- int i, k, g, idx = 0;
- const int c = 1024 / ics->num_windows;
- const uint16_t *offsets = ics->swb_offset;
- float *coef_base = coef;
-
- for (g = 0; g < ics->num_windows; g++)
- memset(coef + g * 128 + offsets[ics->max_sfb], 0,
- sizeof(float) * (c - offsets[ics->max_sfb]));
-
- for (g = 0; g < ics->num_window_groups; g++) {
- unsigned g_len = ics->group_len[g];
-
- for (i = 0; i < ics->max_sfb; i++, idx++) {
- const unsigned cbt_m1 = band_type[idx] - 1;
- float *cfo = coef + offsets[i];
- int off_len = offsets[i + 1] - offsets[i];
- int group;
-
- if (cbt_m1 >= INTENSITY_BT2 - 1) {
- for (group = 0; group < g_len; group++, cfo+=128) {
- memset(cfo, 0, off_len * sizeof(float));
- }
- } else if (cbt_m1 == NOISE_BT - 1) {
- for (group = 0; group < g_len; group++, cfo+=128) {
- float scale;
- float band_energy;
-
- for (k = 0; k < off_len; k++) {
- ac->random_state = lcg_random(ac->random_state);
- cfo[k] = ac->random_state;
- }
-
- band_energy = ac->fdsp.scalarproduct_float(cfo, cfo, off_len);
- scale = sf[idx] / sqrtf(band_energy);
- ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
- }
- } else {
- const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
- const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
- VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
- OPEN_READER(re, gb);
-
- switch (cbt_m1 >> 1) {
- case 0:
- for (group = 0; group < g_len; group++, cfo+=128) {
- float *cf = cfo;
- int len = off_len;
-
- do {
- int code;
- unsigned cb_idx;
-
- UPDATE_CACHE(re, gb);
- GET_VLC(code, re, gb, vlc_tab, 8, 2);
- cb_idx = cb_vector_idx[code];
- cf = VMUL4(cf, vq, cb_idx, sf + idx);
- } while (len -= 4);
- }
- break;
-
- case 1:
- for (group = 0; group < g_len; group++, cfo+=128) {
- float *cf = cfo;
- int len = off_len;
-
- do {
- int code;
- unsigned nnz;
- unsigned cb_idx;
- uint32_t bits;
-
- UPDATE_CACHE(re, gb);
- GET_VLC(code, re, gb, vlc_tab, 8, 2);
- cb_idx = cb_vector_idx[code];
- nnz = cb_idx >> 8 & 15;
- bits = nnz ? GET_CACHE(re, gb) : 0;
- LAST_SKIP_BITS(re, gb, nnz);
- cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
- } while (len -= 4);
- }
- break;
-
- case 2:
- for (group = 0; group < g_len; group++, cfo+=128) {
- float *cf = cfo;
- int len = off_len;
-
- do {
- int code;
- unsigned cb_idx;
-
- UPDATE_CACHE(re, gb);
- GET_VLC(code, re, gb, vlc_tab, 8, 2);
- cb_idx = cb_vector_idx[code];
- cf = VMUL2(cf, vq, cb_idx, sf + idx);
- } while (len -= 2);
- }
- break;
-
- case 3:
- case 4:
- for (group = 0; group < g_len; group++, cfo+=128) {
- float *cf = cfo;
- int len = off_len;
-
- do {
- int code;
- unsigned nnz;
- unsigned cb_idx;
- unsigned sign;
-
- UPDATE_CACHE(re, gb);
- GET_VLC(code, re, gb, vlc_tab, 8, 2);
- cb_idx = cb_vector_idx[code];
- nnz = cb_idx >> 8 & 15;
- sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
- LAST_SKIP_BITS(re, gb, nnz);
- cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
- } while (len -= 2);
- }
- break;
-
- default:
- for (group = 0; group < g_len; group++, cfo+=128) {
- float *cf = cfo;
- uint32_t *icf = (uint32_t *) cf;
- int len = off_len;
-
- do {
- int code;
- unsigned nzt, nnz;
- unsigned cb_idx;
- uint32_t bits;
- int j;
-
- UPDATE_CACHE(re, gb);
- GET_VLC(code, re, gb, vlc_tab, 8, 2);
-
- if (!code) {
- *icf++ = 0;
- *icf++ = 0;
- continue;
- }
-
- cb_idx = cb_vector_idx[code];
- nnz = cb_idx >> 12;
- nzt = cb_idx >> 8;
- bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
- LAST_SKIP_BITS(re, gb, nnz);
-
- for (j = 0; j < 2; j++) {
- if (nzt & 1<<j) {
- uint32_t b;
- int n;
- /* The total length of escape_sequence must be < 22 bits according
- to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
- UPDATE_CACHE(re, gb);
- b = GET_CACHE(re, gb);
- b = 31 - av_log2(~b);
-
- if (b > 8) {
- av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
- return AVERROR_INVALIDDATA;
- }
-
- SKIP_BITS(re, gb, b + 1);
- b += 4;
- n = (1 << b) + SHOW_UBITS(re, gb, b);
- LAST_SKIP_BITS(re, gb, b);
- *icf++ = cbrt_tab[n] | (bits & 1U<<31);
- bits <<= 1;
- } else {
- unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
- *icf++ = (bits & 1U<<31) | v;
- bits <<= !!v;
- }
- cb_idx >>= 4;
- }
- } while (len -= 2);
-
- ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
- }
- }
-
- CLOSE_READER(re, gb);
- }
- }
- coef += g_len << 7;
- }
-
- if (pulse_present) {
- idx = 0;
- for (i = 0; i < pulse->num_pulse; i++) {
- float co = coef_base[ pulse->pos[i] ];
- while (offsets[idx + 1] <= pulse->pos[i])
- idx++;
- if (band_type[idx] != NOISE_BT && sf[idx]) {
- float ico = -pulse->amp[i];
- if (co) {
- co /= sf[idx];
- ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
- }
- coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
- }
- }
- }
- return 0;
-}
-
-static av_always_inline float flt16_round(float pf)
-{
- union av_intfloat32 tmp;
- tmp.f = pf;
- tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
- return tmp.f;
-}
-
-static av_always_inline float flt16_even(float pf)
-{
- union av_intfloat32 tmp;
- tmp.f = pf;
- tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
- return tmp.f;
-}
-
-static av_always_inline float flt16_trunc(float pf)
-{
- union av_intfloat32 pun;
- pun.f = pf;
- pun.i &= 0xFFFF0000U;
- return pun.f;
-}
-
-static av_always_inline void predict(PredictorState *ps, float *coef,
- int output_enable)
-{
- const float a = 0.953125; // 61.0 / 64
- const float alpha = 0.90625; // 29.0 / 32
- float e0, e1;
- float pv;
- float k1, k2;
- float r0 = ps->r0, r1 = ps->r1;
- float cor0 = ps->cor0, cor1 = ps->cor1;
- float var0 = ps->var0, var1 = ps->var1;
-
- k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
- k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
-
- pv = flt16_round(k1 * r0 + k2 * r1);
- if (output_enable)
- *coef += pv;
-
- e0 = *coef;
- e1 = e0 - k1 * r0;
-
- ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
- ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
- ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
- ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
-
- ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
- ps->r0 = flt16_trunc(a * e0);
-}
-
-/**
- * Apply AAC-Main style frequency domain prediction.
- */
-static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
-{
- int sfb, k;
-
- if (!sce->ics.predictor_initialized) {
- reset_all_predictors(sce->predictor_state);
- sce->ics.predictor_initialized = 1;
- }
-
- if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
- for (sfb = 0;
- sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
- sfb++) {
- for (k = sce->ics.swb_offset[sfb];
- k < sce->ics.swb_offset[sfb + 1];
- k++) {
- predict(&sce->predictor_state[k], &sce->coeffs[k],
- sce->ics.predictor_present &&
- sce->ics.prediction_used[sfb]);
- }
- }
- if (sce->ics.predictor_reset_group)
- reset_predictor_group(sce->predictor_state,
- sce->ics.predictor_reset_group);
- } else
- reset_all_predictors(sce->predictor_state);
-}
-
-/**
- * Decode an individual_channel_stream payload; reference: table 4.44.
- *
- * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
- * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_ics(AACContext *ac, SingleChannelElement *sce,
- GetBitContext *gb, int common_window, int scale_flag)
-{
- Pulse pulse;
- TemporalNoiseShaping *tns = &sce->tns;
- IndividualChannelStream *ics = &sce->ics;
- float *out = sce->coeffs;
- int global_gain, pulse_present = 0;
- int ret;
-
- /* This assignment is to silence a GCC warning about the variable being used
- * uninitialized when in fact it always is.
- */
- pulse.num_pulse = 0;
-
- global_gain = get_bits(gb, 8);
-
- if (!common_window && !scale_flag) {
- if (decode_ics_info(ac, ics, gb) < 0)
- return AVERROR_INVALIDDATA;
- }
-
- if ((ret = decode_band_types(ac, sce->band_type,
- sce->band_type_run_end, gb, ics)) < 0)
- return ret;
- if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
- sce->band_type, sce->band_type_run_end)) < 0)
- return ret;
-
- pulse_present = 0;
- if (!scale_flag) {
- if ((pulse_present = get_bits1(gb))) {
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Pulse tool not allowed in eight short sequence.\n");
- return AVERROR_INVALIDDATA;
- }
- if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
- av_log(ac->avctx, AV_LOG_ERROR,
- "Pulse data corrupt or invalid.\n");
- return AVERROR_INVALIDDATA;
- }
- }
- if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
- return AVERROR_INVALIDDATA;
- if (get_bits1(gb)) {
- avpriv_request_sample(ac->avctx, "SSR");
- return AVERROR_PATCHWELCOME;
- }
- }
-
- if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
- &pulse, ics, sce->band_type) < 0)
- return AVERROR_INVALIDDATA;
-
- if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
- apply_prediction(ac, sce);
-
- return 0;
-}
-
-/**
- * Mid/Side stereo decoding; reference: 4.6.8.1.3.
- */
-static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
-{
- const IndividualChannelStream *ics = &cpe->ch[0].ics;
- float *ch0 = cpe->ch[0].coeffs;
- float *ch1 = cpe->ch[1].coeffs;
- int g, i, group, idx = 0;
- const uint16_t *offsets = ics->swb_offset;
- for (g = 0; g < ics->num_window_groups; g++) {
- for (i = 0; i < ics->max_sfb; i++, idx++) {
- if (cpe->ms_mask[idx] &&
- cpe->ch[0].band_type[idx] < NOISE_BT &&
- cpe->ch[1].band_type[idx] < NOISE_BT) {
- for (group = 0; group < ics->group_len[g]; group++) {
- ac->fdsp.butterflies_float(ch0 + group * 128 + offsets[i],
- ch1 + group * 128 + offsets[i],
- offsets[i+1] - offsets[i]);
- }
- }
- }
- ch0 += ics->group_len[g] * 128;
- ch1 += ics->group_len[g] * 128;
- }
-}
-
-/**
- * intensity stereo decoding; reference: 4.6.8.2.3
- *
- * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
- * [1] mask is decoded from bitstream; [2] mask is all 1s;
- * [3] reserved for scalable AAC
- */
-static void apply_intensity_stereo(AACContext *ac,
- ChannelElement *cpe, int ms_present)
-{
- const IndividualChannelStream *ics = &cpe->ch[1].ics;
- SingleChannelElement *sce1 = &cpe->ch[1];
- float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
- const uint16_t *offsets = ics->swb_offset;
- int g, group, i, idx = 0;
- int c;
- float scale;
- for (g = 0; g < ics->num_window_groups; g++) {
- for (i = 0; i < ics->max_sfb;) {
- if (sce1->band_type[idx] == INTENSITY_BT ||
- sce1->band_type[idx] == INTENSITY_BT2) {
- const int bt_run_end = sce1->band_type_run_end[idx];
- for (; i < bt_run_end; i++, idx++) {
- c = -1 + 2 * (sce1->band_type[idx] - 14);
- if (ms_present)
- c *= 1 - 2 * cpe->ms_mask[idx];
- scale = c * sce1->sf[idx];
- for (group = 0; group < ics->group_len[g]; group++)
- ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
- coef0 + group * 128 + offsets[i],
- scale,
- offsets[i + 1] - offsets[i]);
- }
- } else {
- int bt_run_end = sce1->band_type_run_end[idx];
- idx += bt_run_end - i;
- i = bt_run_end;
- }
- }
- coef0 += ics->group_len[g] * 128;
- coef1 += ics->group_len[g] * 128;
- }
-}
-
-/**
- * Decode a channel_pair_element; reference: table 4.4.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
-{
- int i, ret, common_window, ms_present = 0;
-
- common_window = get_bits1(gb);
- if (common_window) {
- if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
- return AVERROR_INVALIDDATA;
- i = cpe->ch[1].ics.use_kb_window[0];
- cpe->ch[1].ics = cpe->ch[0].ics;
- cpe->ch[1].ics.use_kb_window[1] = i;
- if (cpe->ch[1].ics.predictor_present &&
- (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
- if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
- decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
- ms_present = get_bits(gb, 2);
- if (ms_present == 3) {
- av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
- return AVERROR_INVALIDDATA;
- } else if (ms_present)
- decode_mid_side_stereo(cpe, gb, ms_present);
- }
- if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
- return ret;
- if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
- return ret;
-
- if (common_window) {
- if (ms_present)
- apply_mid_side_stereo(ac, cpe);
- if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
- apply_prediction(ac, &cpe->ch[0]);
- apply_prediction(ac, &cpe->ch[1]);
- }
- }
-
- apply_intensity_stereo(ac, cpe, ms_present);
- return 0;
-}
-
-static const float cce_scale[] = {
- 1.09050773266525765921, //2^(1/8)
- 1.18920711500272106672, //2^(1/4)
- M_SQRT2,
- 2,
-};
-
-/**
- * Decode coupling_channel_element; reference: table 4.8.
- *
- * @return Returns error status. 0 - OK, !0 - error
- */
-static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
-{
- int num_gain = 0;
- int c, g, sfb, ret;
- int sign;
- float scale;
- SingleChannelElement *sce = &che->ch[0];
- ChannelCoupling *coup = &che->coup;
-
- coup->coupling_point = 2 * get_bits1(gb);
- coup->num_coupled = get_bits(gb, 3);
- for (c = 0; c <= coup->num_coupled; c++) {
- num_gain++;
- coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
- coup->id_select[c] = get_bits(gb, 4);
- if (coup->type[c] == TYPE_CPE) {
- coup->ch_select[c] = get_bits(gb, 2);
- if (coup->ch_select[c] == 3)
- num_gain++;
- } else
- coup->ch_select[c] = 2;
- }
- coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
-
- sign = get_bits(gb, 1);
- scale = cce_scale[get_bits(gb, 2)];
-
- if ((ret = decode_ics(ac, sce, gb, 0, 0)))
- return ret;
-
- for (c = 0; c < num_gain; c++) {
- int idx = 0;
- int cge = 1;
- int gain = 0;
- float gain_cache = 1.0;
- if (c) {
- cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
- gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
- gain_cache = powf(scale, -gain);
- }
- if (coup->coupling_point == AFTER_IMDCT) {
- coup->gain[c][0] = gain_cache;
- } else {
- for (g = 0; g < sce->ics.num_window_groups; g++) {
- for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
- if (sce->band_type[idx] != ZERO_BT) {
- if (!cge) {
- int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
- if (t) {
- int s = 1;
- t = gain += t;
- if (sign) {
- s -= 2 * (t & 0x1);
- t >>= 1;
- }
- gain_cache = powf(scale, -t) * s;
- }
- }
- coup->gain[c][idx] = gain_cache;
- }
- }
- }
- }
- }
- return 0;
-}
-
-/**
- * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
- *
- * @return Returns number of bytes consumed.
- */
-static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
- GetBitContext *gb)
-{
- int i;
- int num_excl_chan = 0;
-
- do {
- for (i = 0; i < 7; i++)
- che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
- } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
-
- return num_excl_chan / 7;
+{
+ float s = *scale;
+ *dst++ = v[idx & 3] * s;
+ *dst++ = v[idx>>2 & 3] * s;
+ *dst++ = v[idx>>4 & 3] * s;
+ *dst++ = v[idx>>6 & 3] * s;
+ return dst;
}
+#endif
-/**
- * Decode dynamic range information; reference: table 4.52.
- *
- * @return Returns number of bytes consumed.
- */
-static int decode_dynamic_range(DynamicRangeControl *che_drc,
- GetBitContext *gb)
+#ifndef VMUL2S
+static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
+ unsigned sign, const float *scale)
{
- int n = 1;
- int drc_num_bands = 1;
- int i;
-
- /* pce_tag_present? */
- if (get_bits1(gb)) {
- che_drc->pce_instance_tag = get_bits(gb, 4);
- skip_bits(gb, 4); // tag_reserved_bits
- n++;
- }
-
- /* excluded_chns_present? */
- if (get_bits1(gb)) {
- n += decode_drc_channel_exclusions(che_drc, gb);
- }
-
- /* drc_bands_present? */
- if (get_bits1(gb)) {
- che_drc->band_incr = get_bits(gb, 4);
- che_drc->interpolation_scheme = get_bits(gb, 4);
- n++;
- drc_num_bands += che_drc->band_incr;
- for (i = 0; i < drc_num_bands; i++) {
- che_drc->band_top[i] = get_bits(gb, 8);
- n++;
- }
- }
+ union av_intfloat32 s0, s1;
- /* prog_ref_level_present? */
- if (get_bits1(gb)) {
- che_drc->prog_ref_level = get_bits(gb, 7);
- skip_bits1(gb); // prog_ref_level_reserved_bits
- n++;
- }
+ s0.f = s1.f = *scale;
+ s0.i ^= sign >> 1 << 31;
+ s1.i ^= sign << 31;
- for (i = 0; i < drc_num_bands; i++) {
- che_drc->dyn_rng_sgn[i] = get_bits1(gb);
- che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
- n++;
- }
+ *dst++ = v[idx & 15] * s0.f;
+ *dst++ = v[idx>>4 & 15] * s1.f;
- return n;
+ return dst;
}
+#endif
-static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
- uint8_t buf[256];
- int i, major, minor;
-
- if (len < 13+7*8)
- goto unknown;
-
- get_bits(gb, 13); len -= 13;
-
- for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
- buf[i] = get_bits(gb, 8);
+#ifndef VMUL4S
+static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
+ unsigned sign, const float *scale)
+{
+ unsigned nz = idx >> 12;
+ union av_intfloat32 s = { .f = *scale };
+ union av_intfloat32 t;
- buf[i] = 0;
- if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
- av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
+ t.i = s.i ^ (sign & 1U<<31);
+ *dst++ = v[idx & 3] * t.f;
- if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
- ac->avctx->internal->skip_samples = 1024;
- }
+ sign <<= nz & 1; nz >>= 1;
+ t.i = s.i ^ (sign & 1U<<31);
+ *dst++ = v[idx>>2 & 3] * t.f;
-unknown:
- skip_bits_long(gb, len);
+ sign <<= nz & 1; nz >>= 1;
+ t.i = s.i ^ (sign & 1U<<31);
+ *dst++ = v[idx>>4 & 3] * t.f;
- return 0;
-}
+ sign <<= nz & 1;
+ t.i = s.i ^ (sign & 1U<<31);
+ *dst++ = v[idx>>6 & 3] * t.f;
-/**
- * Decode extension data (incomplete); reference: table 4.51.
- *
- * @param cnt length of TYPE_FIL syntactic element in bytes
- *
- * @return Returns number of bytes consumed
- */
-static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
- ChannelElement *che, enum RawDataBlockType elem_type)
-{
- int crc_flag = 0;
- int res = cnt;
- switch (get_bits(gb, 4)) { // extension type
- case EXT_SBR_DATA_CRC:
- crc_flag++;
- case EXT_SBR_DATA:
- if (!che) {
- av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
- return res;
- } else if (!ac->oc[1].m4ac.sbr) {
- av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
- skip_bits_long(gb, 8 * cnt - 4);
- return res;
- } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
- av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
- skip_bits_long(gb, 8 * cnt - 4);
- return res;
- } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
- ac->oc[1].m4ac.sbr = 1;
- ac->oc[1].m4ac.ps = 1;
- output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
- ac->oc[1].status, 1);
- } else {
- ac->oc[1].m4ac.sbr = 1;
- }
- res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
- break;
- case EXT_DYNAMIC_RANGE:
- res = decode_dynamic_range(&ac->che_drc, gb);
- break;
- case EXT_FILL:
- decode_fill(ac, gb, 8 * cnt - 4);
- break;
- case EXT_FILL_DATA:
- case EXT_DATA_ELEMENT:
- default:
- skip_bits_long(gb, 8 * cnt - 4);
- break;
- };
- return res;
+ return dst;
}
+#endif
-/**
- * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
- *
- * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
- * @param coef spectral coefficients
- */
-static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
- IndividualChannelStream *ics, int decode)
+static av_always_inline float flt16_round(float pf)
{
- const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
- int w, filt, m, i;
- int bottom, top, order, start, end, size, inc;
- float lpc[TNS_MAX_ORDER];
- float tmp[TNS_MAX_ORDER+1];
-
- for (w = 0; w < ics->num_windows; w++) {
- bottom = ics->num_swb;
- for (filt = 0; filt < tns->n_filt[w]; filt++) {
- top = bottom;
- bottom = FFMAX(0, top - tns->length[w][filt]);
- order = tns->order[w][filt];
- if (order == 0)
- continue;
-
- // tns_decode_coef
- compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
-
- start = ics->swb_offset[FFMIN(bottom, mmm)];
- end = ics->swb_offset[FFMIN( top, mmm)];
- if ((size = end - start) <= 0)
- continue;
- if (tns->direction[w][filt]) {
- inc = -1;
- start = end - 1;
- } else {
- inc = 1;
- }
- start += w * 128;
-
- if (decode) {
- // ar filter
- for (m = 0; m < size; m++, start += inc)
- for (i = 1; i <= FFMIN(m, order); i++)
- coef[start] -= coef[start - i * inc] * lpc[i - 1];
- } else {
- // ma filter
- for (m = 0; m < size; m++, start += inc) {
- tmp[0] = coef[start];
- for (i = 1; i <= FFMIN(m, order); i++)
- coef[start] += tmp[i] * lpc[i - 1];
- for (i = order; i > 0; i--)
- tmp[i] = tmp[i - 1];
- }
- }
- }
- }
+ union av_intfloat32 tmp;
+ tmp.f = pf;
+ tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
+ return tmp.f;
}
-/**
- * Apply windowing and MDCT to obtain the spectral
- * coefficient from the predicted sample by LTP.
- */
-static void windowing_and_mdct_ltp(AACContext *ac, float *out,
- float *in, IndividualChannelStream *ics)
+static av_always_inline float flt16_even(float pf)
{
- const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
- const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
- const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
- const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
-
- if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
- ac->fdsp.vector_fmul(in, in, lwindow_prev, 1024);
- } else {
- memset(in, 0, 448 * sizeof(float));
- ac->fdsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
- }
- if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
- ac->fdsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
- } else {
- ac->fdsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
- memset(in + 1024 + 576, 0, 448 * sizeof(float));
- }
- ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
+ union av_intfloat32 tmp;
+ tmp.f = pf;
+ tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
+ return tmp.f;
}
-/**
- * Apply the long term prediction
- */
-static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
+static av_always_inline float flt16_trunc(float pf)
{
- const LongTermPrediction *ltp = &sce->ics.ltp;
- const uint16_t *offsets = sce->ics.swb_offset;
- int i, sfb;
-
- if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
- float *predTime = sce->ret;
- float *predFreq = ac->buf_mdct;
- int16_t num_samples = 2048;
-
- if (ltp->lag < 1024)
- num_samples = ltp->lag + 1024;
- for (i = 0; i < num_samples; i++)
- predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
- memset(&predTime[i], 0, (2048 - i) * sizeof(float));
-
- ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
-
- if (sce->tns.present)
- ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
-
- for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
- if (ltp->used[sfb])
- for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
- sce->coeffs[i] += predFreq[i];
- }
+ union av_intfloat32 pun;
+ pun.f = pf;
+ pun.i &= 0xFFFF0000U;
+ return pun.f;
}
-/**
- * Update the LTP buffer for next frame
- */
-static void update_ltp(AACContext *ac, SingleChannelElement *sce)
+static av_always_inline void predict(PredictorState *ps, float *coef,
+ int output_enable)
{
- IndividualChannelStream *ics = &sce->ics;
- float *saved = sce->saved;
- float *saved_ltp = sce->coeffs;
- const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
- const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
- int i;
+ const float a = 0.953125; // 61.0 / 64
+ const float alpha = 0.90625; // 29.0 / 32
+ float e0, e1;
+ float pv;
+ float k1, k2;
+ float r0 = ps->r0, r1 = ps->r1;
+ float cor0 = ps->cor0, cor1 = ps->cor1;
+ float var0 = ps->var0, var1 = ps->var1;
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- memcpy(saved_ltp, saved, 512 * sizeof(float));
- memset(saved_ltp + 576, 0, 448 * sizeof(float));
- ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
- for (i = 0; i < 64; i++)
- saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
- } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
- memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
- memset(saved_ltp + 576, 0, 448 * sizeof(float));
- ac->fdsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
- for (i = 0; i < 64; i++)
- saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
- } else { // LONG_STOP or ONLY_LONG
- ac->fdsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
- for (i = 0; i < 512; i++)
- saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
- }
+ k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
+ k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
- memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
- memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
- memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
-}
+ pv = flt16_round(k1 * r0 + k2 * r1);
+ if (output_enable)
+ *coef += pv;
-/**
- * Conduct IMDCT and windowing.
- */
-static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
-{
- IndividualChannelStream *ics = &sce->ics;
- float *in = sce->coeffs;
- float *out = sce->ret;
- float *saved = sce->saved;
- const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
- const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
- const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
- float *buf = ac->buf_mdct;
- float *temp = ac->temp;
- int i;
+ e0 = *coef;
+ e1 = e0 - k1 * r0;
- // imdct
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- for (i = 0; i < 1024; i += 128)
- ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
- } else
- ac->mdct.imdct_half(&ac->mdct, buf, in);
-
- /* window overlapping
- * NOTE: To simplify the overlapping code, all 'meaningless' short to long
- * and long to short transitions are considered to be short to short
- * transitions. This leaves just two cases (long to long and short to short)
- * with a little special sauce for EIGHT_SHORT_SEQUENCE.
- */
- if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
- (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
- ac->fdsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
- } else {
- memcpy( out, saved, 448 * sizeof(float));
-
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- ac->fdsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
- ac->fdsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
- ac->fdsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
- ac->fdsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
- ac->fdsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
- memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
- } else {
- ac->fdsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
- memcpy( out + 576, buf + 64, 448 * sizeof(float));
- }
- }
+ ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
+ ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
+ ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
+ ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
- // buffer update
- if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
- memcpy( saved, temp + 64, 64 * sizeof(float));
- ac->fdsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
- ac->fdsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
- ac->fdsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
- memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
- } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
- memcpy( saved, buf + 512, 448 * sizeof(float));
- memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
- } else { // LONG_STOP or ONLY_LONG
- memcpy( saved, buf + 512, 512 * sizeof(float));
- }
+ ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
+ ps->r0 = flt16_trunc(a * e0);
}
+static const float cce_scale[] = {
+ 1.09050773266525765921, //2^(1/8)
+ 1.18920711500272106672, //2^(1/4)
+ M_SQRT2,
+ 2,
+};
+
/**
* Apply dependent channel coupling (applied before IMDCT).
*
@@ -2420,385 +263,7 @@ static void apply_independent_coupling(AACContext *ac,
dest[i] += gain * src[i];
}
-/**
- * channel coupling transformation interface
- *
- * @param apply_coupling_method pointer to (in)dependent coupling function
- */
-static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
- enum RawDataBlockType type, int elem_id,
- enum CouplingPoint coupling_point,
- void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
-{
- int i, c;
-
- for (i = 0; i < MAX_ELEM_ID; i++) {
- ChannelElement *cce = ac->che[TYPE_CCE][i];
- int index = 0;
-
- if (cce && cce->coup.coupling_point == coupling_point) {
- ChannelCoupling *coup = &cce->coup;
-
- for (c = 0; c <= coup->num_coupled; c++) {
- if (coup->type[c] == type && coup->id_select[c] == elem_id) {
- if (coup->ch_select[c] != 1) {
- apply_coupling_method(ac, &cc->ch[0], cce, index);
- if (coup->ch_select[c] != 0)
- index++;
- }
- if (coup->ch_select[c] != 2)
- apply_coupling_method(ac, &cc->ch[1], cce, index++);
- } else
- index += 1 + (coup->ch_select[c] == 3);
- }
- }
- }
-}
-
-/**
- * Convert spectral data to float samples, applying all supported tools as appropriate.
- */
-static void spectral_to_sample(AACContext *ac)
-{
- int i, type;
- for (type = 3; type >= 0; type--) {
- for (i = 0; i < MAX_ELEM_ID; i++) {
- ChannelElement *che = ac->che[type][i];
- if (che) {
- if (type <= TYPE_CPE)
- apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
- if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
- if (che->ch[0].ics.predictor_present) {
- if (che->ch[0].ics.ltp.present)
- ac->apply_ltp(ac, &che->ch[0]);
- if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
- ac->apply_ltp(ac, &che->ch[1]);
- }
- }
- if (che->ch[0].tns.present)
- ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
- if (che->ch[1].tns.present)
- ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
- if (type <= TYPE_CPE)
- apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
- if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
- ac->imdct_and_windowing(ac, &che->ch[0]);
- if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
- ac->update_ltp(ac, &che->ch[0]);
- if (type == TYPE_CPE) {
- ac->imdct_and_windowing(ac, &che->ch[1]);
- if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
- ac->update_ltp(ac, &che->ch[1]);
- }
- if (ac->oc[1].m4ac.sbr > 0) {
- ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
- }
- }
- if (type <= TYPE_CCE)
- apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
- }
- }
- }
-}
-
-static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
-{
- int size;
- AACADTSHeaderInfo hdr_info;
- uint8_t layout_map[MAX_ELEM_ID*4][3];
- int layout_map_tags, ret;
-
- size = avpriv_aac_parse_header(gb, &hdr_info);
- if (size > 0) {
- if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
- // This is 2 for "VLB " audio in NSV files.
- // See samples/nsv/vlb_audio.
- avpriv_report_missing_feature(ac->avctx,
- "More than one AAC RDB per ADTS frame");
- ac->warned_num_aac_frames = 1;
- }
- push_output_configuration(ac);
- if (hdr_info.chan_config) {
- ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
- if ((ret = set_default_channel_config(ac->avctx,
- layout_map,
- &layout_map_tags,
- hdr_info.chan_config)) < 0)
- return ret;
- if ((ret = output_configure(ac, layout_map, layout_map_tags,
- FFMAX(ac->oc[1].status,
- OC_TRIAL_FRAME), 0)) < 0)
- return ret;
- } else {
- ac->oc[1].m4ac.chan_config = 0;
- /**
- * dual mono frames in Japanese DTV can have chan_config 0
- * WITHOUT specifying PCE.
- * thus, set dual mono as default.
- */
- if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
- layout_map_tags = 2;
- layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
- layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
- layout_map[0][1] = 0;
- layout_map[1][1] = 1;
- if (output_configure(ac, layout_map, layout_map_tags,
- OC_TRIAL_FRAME, 0))
- return -7;
- }
- }
- ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
- ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
- ac->oc[1].m4ac.object_type = hdr_info.object_type;
- if (ac->oc[0].status != OC_LOCKED ||
- ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
- ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
- ac->oc[1].m4ac.sbr = -1;
- ac->oc[1].m4ac.ps = -1;
- }
- if (!hdr_info.crc_absent)
- skip_bits(gb, 16);
- }
- return size;
-}
-
-static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
- int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
-{
- AACContext *ac = avctx->priv_data;
- ChannelElement *che = NULL, *che_prev = NULL;
- enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
- int err, elem_id;
- int samples = 0, multiplier, audio_found = 0, pce_found = 0;
- int is_dmono, sce_count = 0;
-
- ac->frame = data;
-
- if (show_bits(gb, 12) == 0xfff) {
- if ((err = parse_adts_frame_header(ac, gb)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
- goto fail;
- }
- if (ac->oc[1].m4ac.sampling_index > 12) {
- av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
- }
-
- if ((err = frame_configure_elements(avctx)) < 0)
- goto fail;
-
- ac->tags_mapped = 0;
- // parse
- while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
- elem_id = get_bits(gb, 4);
-
- if (elem_type < TYPE_DSE) {
- if (!(che=get_che(ac, elem_type, elem_id))) {
- av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
- elem_type, elem_id);
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
- samples = 1024;
- }
-
- switch (elem_type) {
-
- case TYPE_SCE:
- err = decode_ics(ac, &che->ch[0], gb, 0, 0);
- audio_found = 1;
- sce_count++;
- break;
-
- case TYPE_CPE:
- err = decode_cpe(ac, gb, che);
- audio_found = 1;
- break;
-
- case TYPE_CCE:
- err = decode_cce(ac, gb, che);
- break;
-
- case TYPE_LFE:
- err = decode_ics(ac, &che->ch[0], gb, 0, 0);
- audio_found = 1;
- break;
-
- case TYPE_DSE:
- err = skip_data_stream_element(ac, gb);
- break;
-
- case TYPE_PCE: {
- uint8_t layout_map[MAX_ELEM_ID*4][3];
- int tags;
- push_output_configuration(ac);
- tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
- if (tags < 0) {
- err = tags;
- break;
- }
- if (pce_found) {
- av_log(avctx, AV_LOG_ERROR,
- "Not evaluating a further program_config_element as this construct is dubious at best.\n");
- } else {
- err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
- if (!err)
- ac->oc[1].m4ac.chan_config = 0;
- pce_found = 1;
- }
- break;
- }
-
- case TYPE_FIL:
- if (elem_id == 15)
- elem_id += get_bits(gb, 8) - 1;
- if (get_bits_left(gb) < 8 * elem_id) {
- av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
- while (elem_id > 0)
- elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
- err = 0; /* FIXME */
- break;
-
- default:
- err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
- break;
- }
-
- che_prev = che;
- elem_type_prev = elem_type;
-
- if (err)
- goto fail;
-
- if (get_bits_left(gb) < 3) {
- av_log(avctx, AV_LOG_ERROR, overread_err);
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
- }
-
- spectral_to_sample(ac);
-
- multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
- samples <<= multiplier;
- /* for dual-mono audio (SCE + SCE) */
- is_dmono = ac->dmono_mode && sce_count == 2 &&
- ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
-
- if (samples)
- ac->frame->nb_samples = samples;
- else
- av_frame_unref(ac->frame);
- *got_frame_ptr = !!samples;
-
- if (is_dmono) {
- if (ac->dmono_mode == 1)
- ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
- else if (ac->dmono_mode == 2)
- ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
- }
-
- if (ac->oc[1].status && audio_found) {
- avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
- avctx->frame_size = samples;
- ac->oc[1].status = OC_LOCKED;
- }
-
- if (multiplier) {
- int side_size;
- const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
- if (side && side_size>=4)
- AV_WL32(side, 2*AV_RL32(side));
- }
- return 0;
-fail:
- pop_output_configuration(ac);
- return err;
-}
-
-static int aac_decode_frame(AVCodecContext *avctx, void *data,
- int *got_frame_ptr, AVPacket *avpkt)
-{
- AACContext *ac = avctx->priv_data;
- const uint8_t *buf = avpkt->data;
- int buf_size = avpkt->size;
- GetBitContext gb;
- int buf_consumed;
- int buf_offset;
- int err;
- int new_extradata_size;
- const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
- AV_PKT_DATA_NEW_EXTRADATA,
- &new_extradata_size);
- int jp_dualmono_size;
- const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
- AV_PKT_DATA_JP_DUALMONO,
- &jp_dualmono_size);
-
- if (new_extradata && 0) {
- av_free(avctx->extradata);
- avctx->extradata = av_mallocz(new_extradata_size +
- FF_INPUT_BUFFER_PADDING_SIZE);
- if (!avctx->extradata)
- return AVERROR(ENOMEM);
- avctx->extradata_size = new_extradata_size;
- memcpy(avctx->extradata, new_extradata, new_extradata_size);
- push_output_configuration(ac);
- if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
- avctx->extradata,
- avctx->extradata_size*8, 1) < 0) {
- pop_output_configuration(ac);
- return AVERROR_INVALIDDATA;
- }
- }
-
- ac->dmono_mode = 0;
- if (jp_dualmono && jp_dualmono_size > 0)
- ac->dmono_mode = 1 + *jp_dualmono;
- if (ac->force_dmono_mode >= 0)
- ac->dmono_mode = ac->force_dmono_mode;
-
- if (INT_MAX / 8 <= buf_size)
- return AVERROR_INVALIDDATA;
-
- if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0)
- return err;
-
- if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt)) < 0)
- return err;
-
- buf_consumed = (get_bits_count(&gb) + 7) >> 3;
- for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
- if (buf[buf_offset])
- break;
-
- return buf_size > buf_offset ? buf_consumed : buf_size;
-}
-
-static av_cold int aac_decode_close(AVCodecContext *avctx)
-{
- AACContext *ac = avctx->priv_data;
- int i, type;
-
- for (i = 0; i < MAX_ELEM_ID; i++) {
- for (type = 0; type < 4; type++) {
- if (ac->che[type][i])
- ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
- av_freep(&ac->che[type][i]);
- }
- }
-
- ff_mdct_end(&ac->mdct);
- ff_mdct_end(&ac->mdct_small);
- ff_mdct_end(&ac->mdct_ltp);
- return 0;
-}
-
+#include "aacdec_template.c"
#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
@@ -3070,40 +535,6 @@ static av_cold int latm_decode_init(AVCodecContext *avctx)
return ret;
}
-static void aacdec_init(AACContext *c)
-{
- c->imdct_and_windowing = imdct_and_windowing;
- c->apply_ltp = apply_ltp;
- c->apply_tns = apply_tns;
- c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
- c->update_ltp = update_ltp;
-
- if(ARCH_MIPS)
- ff_aacdec_init_mips(c);
-}
-/**
- * AVOptions for Japanese DTV specific extensions (ADTS only)
- */
-#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
-static const AVOption options[] = {
- {"dual_mono_mode", "Select the channel to decode for dual mono",
- offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
- AACDEC_FLAGS, "dual_mono_mode"},
-
- {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
- {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
- {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
- {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
-
- {NULL},
-};
-
-static const AVClass aac_decoder_class = {
- .class_name = "AAC decoder",
- .item_name = av_default_item_name,
- .option = options,
- .version = LIBAVUTIL_VERSION_INT,
-};
AVCodec ff_aac_decoder = {
.name = "aac",
diff --git a/libavcodec/aacdec_fixed.c b/libavcodec/aacdec_fixed.c
new file mode 100644
index 0000000..f011bf6
--- /dev/null
+++ b/libavcodec/aacdec_fixed.c
@@ -0,0 +1,458 @@
+/*
+ * Copyright (c) 2013
+ * MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * AAC decoder fixed-point implementation
+ *
+ * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
+ * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * AAC decoder
+ * @author Oded Shimon ( ods15 ods15 dyndns org )
+ * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
+ *
+ * Fixed point implementation
+ * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
+ */
+
+#define CONFIG_FFT_FLOAT 0
+#define CONFIG_FFT_FIXED_32 1
+#define CONFIG_AAC_FIXED 1
+#define CONFIG_FIXED 1
+
+#include "libavutil/fixed_dsp.h"
+#include "libavutil/opt.h"
+#include "avcodec.h"
+#include "internal.h"
+#include "get_bits.h"
+#include "fft.h"
+#include "fmtconvert.h"
+#include "lpc.h"
+#include "kbdwin.h"
+#include "sinewin.h"
+
+#include "aac.h"
+#include "aactab.h"
+#include "aacdectab.h"
+#include "cbrt_tablegen.h"
+#include "sbr.h"
+#include "aacsbr.h"
+#include "mpeg4audio.h"
+#include "aacadtsdec.h"
+#include "libavutil/intfloat.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <math.h>
+#include <string.h>
+
+static av_always_inline void reset_predict_state(PredictorState *ps)
+{
+ ps->r0.mant = 0;
+ ps->r0.expo = 0;
+ ps->r1.mant = 0;
+ ps->r1.expo = 0;
+ ps->cor0.mant = 0;
+ ps->cor0.expo = 0;
+ ps->cor1.mant = 0;
+ ps->cor1.expo = 0;
+ ps->var0.mant = 0x20000000;
+ ps->var0.expo = 1;
+ ps->var1.mant = 0x20000000;
+ ps->var1.expo = 1;
+}
+
+int exp2tab[4] = { Q31(1.0000000000/2), Q31(1.1892071150/2), Q31(1.4142135624/2), Q31(1.6817928305/2) }; // 2^0, 2^0.25, 2^0.5, 2^0.75
+
+static inline int *DEC_SPAIR(int *dst, unsigned idx)
+{
+ dst[0] = (idx & 15) - 4;
+ dst[1] = (idx>>4 & 15) - 4;
+
+ return dst + 2;
+}
+
+static inline int *DEC_SQUAD(int *dst, unsigned idx)
+{
+ dst[0] = (idx & 3) - 1;
+ dst[1] = (idx>>2 & 3) - 1;
+ dst[2] = (idx>>4 & 3) - 1;
+ dst[3] = (idx>>6 & 3) - 1;
+
+ return dst + 4;
+}
+
+static inline int *DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
+{
+ dst[0] = (idx & 15) * (1 - (((int)sign>>1)<<1));
+ dst[1] = (idx>>4 & 15) * (1 - (((int)sign&1)<<1));
+
+ return dst + 2;
+}
+
+static inline int *DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
+{
+ unsigned nz = idx >> 12;
+
+ dst[0] = (idx & 3) * (1 + (((int)sign>>31)<<1));
+ sign <<= nz & 1; nz >>= 1;
+ dst[1] = (idx>>2 & 3) * (1 + (((int)sign>>31)<<1));
+ sign <<= nz & 1; nz >>= 1;
+ dst[2] = (idx>>4 & 3) * (1 + (((int)sign>>31)<<1));
+ sign <<= nz & 1; nz >>= 1;
+ dst[3] = (idx>>6 & 3) * (1 + (((int)sign>>31)<<1));
+
+ return dst + 4;
+}
+
+static void vector_pow43(int *coefs, int len)
+{
+ int i, coef;
+
+ for (i=0; i<len; i++) {
+ coef = coefs[i];
+ if (coef < 0)
+ coef = -(int)cbrt_tab[-coef];
+ else
+ coef = (int)cbrt_tab[coef];
+ coefs[i] = coef;
+ }
+}
+
+static void subband_scale(int *dst, int *src, int scale, int offset, int len)
+{
+ int64_t accu;
+ int ssign = scale < 0 ? -1 : 1;
+ int s = FFABS(scale);
+ unsigned int round;
+ int i, out, c = exp2tab[s & 3];
+
+ s = offset - (s >> 2);
+
+ if (s > 0) {
+ round = 1 << (s-1);
+ for (i=0; i<len; i++) {
+ accu = (int64_t)src[i] * c;
+ out = (int)(accu >> 32);
+ dst[i] = ((int)(out+round) >> s) * ssign;
+ }
+ }
+ else {
+ s = s + 32;
+ round = 1 << (s-1);
+ for (i=0; i<len; i++) {
+ accu = (int64_t)src[i] * c;
+ out = (int)((int64_t)(accu+round) >> s);
+ dst[i] = out * ssign;
+ }
+ }
+}
+
+static void noise_scale(int *coefs, int scale, int band_energy, int len)
+{
+ int64_t accu;
+ int ssign = scale < 0 ? -1 : 1;
+ int s = FFABS(scale);
+ unsigned int round;
+ int i, out, c = exp2tab[s & 3];
+ int nlz = 0;
+
+ while (band_energy > 0x7fff) {
+ band_energy >>= 1;
+ nlz++;
+ }
+ c /= band_energy;
+ s = 21 + nlz - (s >> 2);
+
+ if (s > 0) {
+ round = 1 << (s-1);
+ for (i=0; i<len; i++) {
+ accu = (int64_t)coefs[i] * c;
+ out = (int)(accu >> 32);
+ coefs[i] = ((int)(out+round) >> s) * ssign;
+ }
+ }
+ else {
+ s = s + 32;
+ round = 1 << (s-1);
+ for (i=0; i<len; i++) {
+ accu = (int64_t)coefs[i] * c;
+ out = (int)((int64_t)(accu+round) >> s);
+ coefs[i] = out * ssign;
+ }
+ }
+}
+
+static av_always_inline aac_float_t flt16_round(aac_float_t pf)
+{
+ aac_float_t tmp;
+ int s;
+
+ tmp.expo = pf.expo;
+ s = pf.mant >> 31;
+ tmp.mant = (pf.mant ^ s) - s;
+ tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
+ tmp.mant = (tmp.mant ^ s) - s;
+
+ return tmp;
+}
+
+static av_always_inline aac_float_t flt16_even(aac_float_t pf)
+{
+ aac_float_t tmp;
+ int s;
+
+ tmp.expo = pf.expo;
+ s = pf.mant >> 31;
+ tmp.mant = (pf.mant ^ s) - s;
+ tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
+ tmp.mant = (tmp.mant ^ s) - s;
+
+ return tmp;
+}
+
+static av_always_inline aac_float_t flt16_trunc(aac_float_t pf)
+{
+ aac_float_t pun;
+ int s;
+
+ pun.expo = pf.expo;
+ s = pf.mant >> 31;
+ pun.mant = (pf.mant ^ s) - s;
+ pun.mant = pun.mant & 0xFFC00000U;
+ pun.mant = (pun.mant ^ s) - s;
+
+ return pun;
+}
+
+static av_always_inline void predict(PredictorState *ps, int *coef,
+ int output_enable)
+{
+ const aac_float_t a = { 1023410176, 0 }; // 61.0 / 64
+ const aac_float_t alpha = { 973078528, 0 }; // 29.0 / 32
+ aac_float_t e0, e1;
+ aac_float_t pv;
+ aac_float_t k1, k2;
+ aac_float_t r0 = ps->r0, r1 = ps->r1;
+ aac_float_t cor0 = ps->cor0, cor1 = ps->cor1;
+ aac_float_t var0 = ps->var0, var1 = ps->var1;
+ aac_float_t tmp;
+
+ if (var0.expo > 1 || (var0.expo == 1 && var0.mant > 0x20000000)) {
+ tmp = float_recip(var0);
+ k1 = float_mul(cor0, flt16_even(float_mul(a, tmp)));
+ }
+ else {
+ k1.mant = 0;
+ k1.expo = 0;
+ }
+
+ if (var1.expo > 1 || (var1.expo == 1 && var1.mant > 0x20000000)) {
+ tmp = float_recip(var1);
+ k2 = float_mul(cor1, flt16_even(float_mul(a, tmp)));
+ }
+ else {
+ k2.mant = 0;
+ k2.expo = 0;
+ }
+
+ tmp = float_mul(k1, r0);
+ pv = flt16_round(float_add(tmp, float_mul(k2, r1)));
+ if (output_enable) {
+ int shift = 28 - pv.expo;
+
+ if (shift < 31)
+ *coef += (pv.mant + (1<<(shift-1))) >> shift;
+ }
+
+ e0 = int2float(*coef, 28);
+ e1 = float_sub(e0, tmp);
+
+ ps->cor1 = flt16_trunc(float_add(float_mul(alpha, cor1), float_mul(r1, e1)));
+ tmp = float_add(float_mul(r1, r1), float_mul(e1, e1));
+ tmp.expo--;
+ ps->var1 = flt16_trunc(float_add(float_mul(alpha, var1), tmp));
+ ps->cor0 = flt16_trunc(float_add(float_mul(alpha, cor0), float_mul(r0, e0)));
+ tmp = float_add(float_mul(r0, r0), float_mul(e0, e0));
+ tmp.expo--;
+ ps->var0 = flt16_trunc(float_add(float_mul(alpha, var0), tmp));
+
+ ps->r1 = flt16_trunc(float_mul(a, float_sub(r0, float_mul(k1, e0))));
+ ps->r0 = flt16_trunc(float_mul(a, e0));
+}
+
+
+static const int cce_scale_fixed[8] = {
+ Q30(1.0), //2^(0/8)
+ Q30(1.0905077327), //2^(1/8)
+ Q30(1.1892071150), //2^(2/8)
+ Q30(1.2968395547), //2^(3/8)
+ Q30(1.4142135624), //2^(4/8)
+ Q30(1.5422108254), //2^(5/8)
+ Q30(1.6817928305), //2^(6/8)
+ Q30(1.8340080864), //2^(7/8)
+};
+
+/**
+ * Apply dependent channel coupling (applied before IMDCT).
+ *
+ * @param index index into coupling gain array
+ */
+static void apply_dependent_coupling_fixed(AACContext *ac,
+ SingleChannelElement *target,
+ ChannelElement *cce, int index)
+{
+ IndividualChannelStream *ics = &cce->ch[0].ics;
+ const uint16_t *offsets = ics->swb_offset;
+ int *dest = target->coeffs;
+ const int *src = cce->ch[0].coeffs;
+ int g, i, group, k, idx = 0;
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Dependent coupling is not supported together with LTP\n");
+ return;
+ }
+ for (g = 0; g < ics->num_window_groups; g++) {
+ for (i = 0; i < ics->max_sfb; i++, idx++) {
+ if (cce->ch[0].band_type[idx] != ZERO_BT) {
+ const int gain = cce->coup.gain[index][idx];
+ int shift, round, c, tmp;
+ int64_t accu;
+
+ if (gain < 0) {
+ c = -cce_scale_fixed[-gain & 7];
+ shift = (-gain-1024) >> 3;
+ }
+ else {
+ c = cce_scale_fixed[gain & 7];
+ shift = (gain-1024) >> 3;
+ }
+
+ if (shift < 0) {
+ shift = -shift;
+ round = 1 << (shift - 1);
+
+ for (group = 0; group < ics->group_len[g]; group++) {
+ for (k = offsets[i]; k < offsets[i + 1]; k++) {
+ // XXX dsputil-ize
+ accu = (int64_t)src[group * 128 + k]*c;
+ tmp = (int)((accu + 0x20000000) >> 30);
+ dest[group * 128 + k] += (tmp + round) >> shift;
+ }
+ }
+ }
+ else {
+ for (group = 0; group < ics->group_len[g]; group++) {
+ for (k = offsets[i]; k < offsets[i + 1]; k++) {
+ // XXX dsputil-ize
+ accu = (int64_t)src[group * 128 + k]*c;
+ tmp = (int)((accu + 0x20000000) >> 30);
+ dest[group * 128 + k] += tmp << shift;
+ }
+ }
+ }
+ }
+ }
+ dest += ics->group_len[g] * 128;
+ src += ics->group_len[g] * 128;
+ }
+}
+
+/**
+ * Apply independent channel coupling (applied after IMDCT).
+ *
+ * @param index index into coupling gain array
+ */
+static void apply_independent_coupling_fixed(AACContext *ac,
+ SingleChannelElement *target,
+ ChannelElement *cce, int index)
+{
+ int i, c, shift, round, tmp;
+ const int gain = cce->coup.gain[index][0];
+ const int *src = cce->ch[0].ret;
+ int *dest = target->ret;
+ const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
+ int64_t accu;
+
+ c = cce_scale_fixed[gain & 7];
+ shift = (gain-1024) >> 3;
+ if (shift < 0) {
+ shift = -shift;
+ round = 1 << (shift - 1);
+
+ for (i = 0; i < len; i++) {
+ accu = (long long)src[i]*c;
+ tmp = (int)((accu + 0x20000000) >> 30);
+ dest[i] += (tmp + round) >> shift;
+ }
+ }
+ else {
+ for (i = 0; i < len; i++) {
+ accu = (long long)src[i]*c;
+ tmp = (int)((accu + 0x20000000) >> 30);
+ dest[i] += tmp << shift;
+ }
+ }
+}
+
+#include "aacdec_template.c"
+
+AVCodec ff_aac_fixed_decoder = {
+ .name = "aac_fixed",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_AAC,
+ .priv_data_size = sizeof(AACContext),
+ .init = aac_decode_init,
+ .close = aac_decode_close,
+ .decode = aac_decode_frame,
+ .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
+ .sample_fmts = (const enum AVSampleFormat[]) {
+ AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
+ },
+ .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
+ .channel_layouts = aac_channel_layout,
+ .flush = flush,
+};
diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c
new file mode 100644
index 0000000..ea68c14
--- /dev/null
+++ b/libavcodec/aacdec_template.c
@@ -0,0 +1,2747 @@
+/*
+ * AAC decoder
+ * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
+ * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
+ *
+ * AAC LATM decoder
+ * Copyright (c) 2008-2010 Paul Kendall <paul at kcbbs.gen.nz>
+ * Copyright (c) 2010 Janne Grunau <janne-libav at jannau.net>
+ *
+ * AAC decoder fixed-point implementation
+ * Copyright (c) 2013
+ * MIPS Technologies, Inc., California.
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * AAC decoder
+ * @author Oded Shimon ( ods15 ods15 dyndns org )
+ * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
+ *
+ * AAC decoder fixed-point implementation
+ * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
+ * @author Nedeljko Babic ( nedeljko.babic imgtec com )
+ */
+
+/*
+ * supported tools
+ *
+ * Support? Name
+ * N (code in SoC repo) gain control
+ * Y block switching
+ * Y window shapes - standard
+ * N window shapes - Low Delay
+ * Y filterbank - standard
+ * N (code in SoC repo) filterbank - Scalable Sample Rate
+ * Y Temporal Noise Shaping
+ * Y Long Term Prediction
+ * Y intensity stereo
+ * Y channel coupling
+ * Y frequency domain prediction
+ * Y Perceptual Noise Substitution
+ * Y Mid/Side stereo
+ * N Scalable Inverse AAC Quantization
+ * N Frequency Selective Switch
+ * N upsampling filter
+ * Y quantization & coding - AAC
+ * N quantization & coding - TwinVQ
+ * N quantization & coding - BSAC
+ * N AAC Error Resilience tools
+ * N Error Resilience payload syntax
+ * N Error Protection tool
+ * N CELP
+ * N Silence Compression
+ * N HVXC
+ * N HVXC 4kbits/s VR
+ * N Structured Audio tools
+ * N Structured Audio Sample Bank Format
+ * N MIDI
+ * N Harmonic and Individual Lines plus Noise
+ * N Text-To-Speech Interface
+ * Y Spectral Band Replication
+ * Y (not in this code) Layer-1
+ * Y (not in this code) Layer-2
+ * Y (not in this code) Layer-3
+ * N SinuSoidal Coding (Transient, Sinusoid, Noise)
+ * Y Parametric Stereo
+ * N Direct Stream Transfer
+ *
+ * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
+ * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
+ Parametric Stereo.
+ */
+
+
+static VLC vlc_scalefactors;
+static VLC vlc_spectral[11];
+
+static int output_configure(AACContext *ac,
+ uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
+ enum OCStatus oc_type, int get_new_frame);
+
+#define overread_err "Input buffer exhausted before END element found\n"
+
+static int count_channels(uint8_t (*layout)[3], int tags)
+{
+ int i, sum = 0;
+ for (i = 0; i < tags; i++) {
+ int syn_ele = layout[i][0];
+ int pos = layout[i][2];
+ sum += (1 + (syn_ele == TYPE_CPE)) *
+ (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
+ }
+ return sum;
+}
+
+/**
+ * Check for the channel element in the current channel position configuration.
+ * If it exists, make sure the appropriate element is allocated and map the
+ * channel order to match the internal FFmpeg channel layout.
+ *
+ * @param che_pos current channel position configuration
+ * @param type channel element type
+ * @param id channel element id
+ * @param channels count of the number of channels in the configuration
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static av_cold int che_configure(AACContext *ac,
+ enum ChannelPosition che_pos,
+ int type, int id, int *channels)
+{
+ if (*channels >= MAX_CHANNELS)
+ return AVERROR_INVALIDDATA;
+ if (che_pos) {
+ if (!ac->che[type][id]) {
+ if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
+ return AVERROR(ENOMEM);
+ AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr);
+ }
+ if (type != TYPE_CCE) {
+ if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
+ av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
+ return AVERROR_INVALIDDATA;
+ }
+ ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
+ if (type == TYPE_CPE ||
+ (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
+ ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
+ }
+ }
+ } else {
+ if (ac->che[type][id])
+ AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][id]->sbr);
+ av_freep(&ac->che[type][id]);
+ }
+ return 0;
+}
+
+static int frame_configure_elements(AVCodecContext *avctx)
+{
+ AACContext *ac = avctx->priv_data;
+ int type, id, ch, ret;
+
+ /* set channel pointers to internal buffers by default */
+ for (type = 0; type < 4; type++) {
+ for (id = 0; id < MAX_ELEM_ID; id++) {
+ ChannelElement *che = ac->che[type][id];
+ if (che) {
+ che->ch[0].ret = che->ch[0].ret_buf;
+ che->ch[1].ret = che->ch[1].ret_buf;
+ }
+ }
+ }
+
+ /* get output buffer */
+ av_frame_unref(ac->frame);
+ ac->frame->nb_samples = 2048;
+ if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
+ return ret;
+
+ /* map output channel pointers to AVFrame data */
+ for (ch = 0; ch < avctx->channels; ch++) {
+ if (ac->output_element[ch])
+ ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
+ }
+
+ return 0;
+}
+
+struct elem_to_channel {
+ uint64_t av_position;
+ uint8_t syn_ele;
+ uint8_t elem_id;
+ uint8_t aac_position;
+};
+
+static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
+ uint8_t (*layout_map)[3], int offset, uint64_t left,
+ uint64_t right, int pos)
+{
+ if (layout_map[offset][0] == TYPE_CPE) {
+ e2c_vec[offset] = (struct elem_to_channel) {
+ .av_position = left | right,
+ .syn_ele = TYPE_CPE,
+ .elem_id = layout_map[offset][1],
+ .aac_position = pos
+ };
+ return 1;
+ } else {
+ e2c_vec[offset] = (struct elem_to_channel) {
+ .av_position = left,
+ .syn_ele = TYPE_SCE,
+ .elem_id = layout_map[offset][1],
+ .aac_position = pos
+ };
+ e2c_vec[offset + 1] = (struct elem_to_channel) {
+ .av_position = right,
+ .syn_ele = TYPE_SCE,
+ .elem_id = layout_map[offset + 1][1],
+ .aac_position = pos
+ };
+ return 2;
+ }
+}
+
+static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
+ int *current)
+{
+ int num_pos_channels = 0;
+ int first_cpe = 0;
+ int sce_parity = 0;
+ int i;
+ for (i = *current; i < tags; i++) {
+ if (layout_map[i][2] != pos)
+ break;
+ if (layout_map[i][0] == TYPE_CPE) {
+ if (sce_parity) {
+ if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
+ sce_parity = 0;
+ } else {
+ return -1;
+ }
+ }
+ num_pos_channels += 2;
+ first_cpe = 1;
+ } else {
+ num_pos_channels++;
+ sce_parity ^= 1;
+ }
+ }
+ if (sce_parity &&
+ ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
+ return -1;
+ *current = i;
+ return num_pos_channels;
+}
+
+static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
+{
+ int i, n, total_non_cc_elements;
+ struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
+ int num_front_channels, num_side_channels, num_back_channels;
+ uint64_t layout;
+
+ if (FF_ARRAY_ELEMS(e2c_vec) < tags)
+ return 0;
+
+ i = 0;
+ num_front_channels =
+ count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
+ if (num_front_channels < 0)
+ return 0;
+ num_side_channels =
+ count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
+ if (num_side_channels < 0)
+ return 0;
+ num_back_channels =
+ count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
+ if (num_back_channels < 0)
+ return 0;
+
+ i = 0;
+ if (num_front_channels & 1) {
+ e2c_vec[i] = (struct elem_to_channel) {
+ .av_position = AV_CH_FRONT_CENTER,
+ .syn_ele = TYPE_SCE,
+ .elem_id = layout_map[i][1],
+ .aac_position = AAC_CHANNEL_FRONT
+ };
+ i++;
+ num_front_channels--;
+ }
+ if (num_front_channels >= 4) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ AV_CH_FRONT_LEFT_OF_CENTER,
+ AV_CH_FRONT_RIGHT_OF_CENTER,
+ AAC_CHANNEL_FRONT);
+ num_front_channels -= 2;
+ }
+ if (num_front_channels >= 2) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ AV_CH_FRONT_LEFT,
+ AV_CH_FRONT_RIGHT,
+ AAC_CHANNEL_FRONT);
+ num_front_channels -= 2;
+ }
+ while (num_front_channels >= 2) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ UINT64_MAX,
+ UINT64_MAX,
+ AAC_CHANNEL_FRONT);
+ num_front_channels -= 2;
+ }
+
+ if (num_side_channels >= 2) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ AV_CH_SIDE_LEFT,
+ AV_CH_SIDE_RIGHT,
+ AAC_CHANNEL_FRONT);
+ num_side_channels -= 2;
+ }
+ while (num_side_channels >= 2) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ UINT64_MAX,
+ UINT64_MAX,
+ AAC_CHANNEL_SIDE);
+ num_side_channels -= 2;
+ }
+
+ while (num_back_channels >= 4) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ UINT64_MAX,
+ UINT64_MAX,
+ AAC_CHANNEL_BACK);
+ num_back_channels -= 2;
+ }
+ if (num_back_channels >= 2) {
+ i += assign_pair(e2c_vec, layout_map, i,
+ AV_CH_BACK_LEFT,
+ AV_CH_BACK_RIGHT,
+ AAC_CHANNEL_BACK);
+ num_back_channels -= 2;
+ }
+ if (num_back_channels) {
+ e2c_vec[i] = (struct elem_to_channel) {
+ .av_position = AV_CH_BACK_CENTER,
+ .syn_ele = TYPE_SCE,
+ .elem_id = layout_map[i][1],
+ .aac_position = AAC_CHANNEL_BACK
+ };
+ i++;
+ num_back_channels--;
+ }
+
+ if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
+ e2c_vec[i] = (struct elem_to_channel) {
+ .av_position = AV_CH_LOW_FREQUENCY,
+ .syn_ele = TYPE_LFE,
+ .elem_id = layout_map[i][1],
+ .aac_position = AAC_CHANNEL_LFE
+ };
+ i++;
+ }
+ while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
+ e2c_vec[i] = (struct elem_to_channel) {
+ .av_position = UINT64_MAX,
+ .syn_ele = TYPE_LFE,
+ .elem_id = layout_map[i][1],
+ .aac_position = AAC_CHANNEL_LFE
+ };
+ i++;
+ }
+
+ // Must choose a stable sort
+ total_non_cc_elements = n = i;
+ do {
+ int next_n = 0;
+ for (i = 1; i < n; i++)
+ if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
+ FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
+ next_n = i;
+ }
+ n = next_n;
+ } while (n > 0);
+
+ layout = 0;
+ for (i = 0; i < total_non_cc_elements; i++) {
+ layout_map[i][0] = e2c_vec[i].syn_ele;
+ layout_map[i][1] = e2c_vec[i].elem_id;
+ layout_map[i][2] = e2c_vec[i].aac_position;
+ if (e2c_vec[i].av_position != UINT64_MAX) {
+ layout |= e2c_vec[i].av_position;
+ }
+ }
+
+ return layout;
+}
+
+/**
+ * Save current output configuration if and only if it has been locked.
+ */
+static void push_output_configuration(AACContext *ac) {
+ if (ac->oc[1].status == OC_LOCKED) {
+ ac->oc[0] = ac->oc[1];
+ }
+ ac->oc[1].status = OC_NONE;
+}
+
+/**
+ * Restore the previous output configuration if and only if the current
+ * configuration is unlocked.
+ */
+static void pop_output_configuration(AACContext *ac) {
+ if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
+ ac->oc[1] = ac->oc[0];
+ ac->avctx->channels = ac->oc[1].channels;
+ ac->avctx->channel_layout = ac->oc[1].channel_layout;
+ output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
+ ac->oc[1].status, 0);
+ }
+}
+
+/**
+ * Configure output channel order based on the current program
+ * configuration element.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int output_configure(AACContext *ac,
+ uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
+ enum OCStatus oc_type, int get_new_frame)
+{
+ AVCodecContext *avctx = ac->avctx;
+ int i, channels = 0, ret;
+ uint64_t layout = 0;
+
+ if (ac->oc[1].layout_map != layout_map) {
+ memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
+ ac->oc[1].layout_map_tags = tags;
+ }
+
+ // Try to sniff a reasonable channel order, otherwise output the
+ // channels in the order the PCE declared them.
+ if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
+ layout = sniff_channel_order(layout_map, tags);
+ for (i = 0; i < tags; i++) {
+ int type = layout_map[i][0];
+ int id = layout_map[i][1];
+ int position = layout_map[i][2];
+ // Allocate or free elements depending on if they are in the
+ // current program configuration.
+ ret = che_configure(ac, position, type, id, &channels);
+ if (ret < 0)
+ return ret;
+ }
+ if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
+ if (layout == AV_CH_FRONT_CENTER) {
+ layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
+ } else {
+ layout = 0;
+ }
+ }
+
+ memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
+ if (layout) avctx->channel_layout = layout;
+ ac->oc[1].channel_layout = layout;
+ avctx->channels = ac->oc[1].channels = channels;
+ ac->oc[1].status = oc_type;
+
+ if (get_new_frame) {
+ if ((ret = frame_configure_elements(ac->avctx)) < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+static void flush(AVCodecContext *avctx)
+{
+ AACContext *ac= avctx->priv_data;
+ int type, i, j;
+
+ for (type = 3; type >= 0; type--) {
+ for (i = 0; i < MAX_ELEM_ID; i++) {
+ ChannelElement *che = ac->che[type][i];
+ if (che) {
+ for (j = 0; j <= 1; j++) {
+ memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Set up channel positions based on a default channel configuration
+ * as specified in table 1.17.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int set_default_channel_config(AVCodecContext *avctx,
+ uint8_t (*layout_map)[3],
+ int *tags,
+ int channel_config)
+{
+ if (channel_config < 1 || channel_config > 7) {
+ av_log(avctx, AV_LOG_ERROR,
+ "invalid default channel configuration (%d)\n",
+ channel_config);
+ return AVERROR_INVALIDDATA;
+ }
+ *tags = tags_per_config[channel_config];
+ memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
+ *tags * sizeof(*layout_map));
+ return 0;
+}
+
+static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
+{
+ /* For PCE based channel configurations map the channels solely based
+ * on tags. */
+ if (!ac->oc[1].m4ac.chan_config) {
+ return ac->tag_che_map[type][elem_id];
+ }
+ // Allow single CPE stereo files to be signalled with mono configuration.
+ if (!ac->tags_mapped && type == TYPE_CPE &&
+ ac->oc[1].m4ac.chan_config == 1) {
+ uint8_t layout_map[MAX_ELEM_ID*4][3];
+ int layout_map_tags;
+ push_output_configuration(ac);
+
+ av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
+
+ if (set_default_channel_config(ac->avctx, layout_map,
+ &layout_map_tags, 2) < 0)
+ return NULL;
+ if (output_configure(ac, layout_map, layout_map_tags,
+ OC_TRIAL_FRAME, 1) < 0)
+ return NULL;
+
+ ac->oc[1].m4ac.chan_config = 2;
+ ac->oc[1].m4ac.ps = 0;
+ }
+ // And vice-versa
+ if (!ac->tags_mapped && type == TYPE_SCE &&
+ ac->oc[1].m4ac.chan_config == 2) {
+ uint8_t layout_map[MAX_ELEM_ID * 4][3];
+ int layout_map_tags;
+ push_output_configuration(ac);
+
+ av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
+
+ if (set_default_channel_config(ac->avctx, layout_map,
+ &layout_map_tags, 1) < 0)
+ return NULL;
+ if (output_configure(ac, layout_map, layout_map_tags,
+ OC_TRIAL_FRAME, 1) < 0)
+ return NULL;
+
+ ac->oc[1].m4ac.chan_config = 1;
+ if (ac->oc[1].m4ac.sbr)
+ ac->oc[1].m4ac.ps = -1;
+ }
+ /* For indexed channel configurations map the channels solely based
+ * on position. */
+ switch (ac->oc[1].m4ac.chan_config) {
+ case 7:
+ if (ac->tags_mapped == 3 && type == TYPE_CPE) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
+ }
+ case 6:
+ /* Some streams incorrectly code 5.1 audio as
+ * SCE[0] CPE[0] CPE[1] SCE[1]
+ * instead of
+ * SCE[0] CPE[0] CPE[1] LFE[0].
+ * If we seem to have encountered such a stream, transfer
+ * the LFE[0] element to the SCE[1]'s mapping */
+ if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
+ }
+ case 5:
+ if (ac->tags_mapped == 2 && type == TYPE_CPE) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
+ }
+ case 4:
+ if (ac->tags_mapped == 2 &&
+ ac->oc[1].m4ac.chan_config == 4 &&
+ type == TYPE_SCE) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
+ }
+ case 3:
+ case 2:
+ if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
+ type == TYPE_CPE) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
+ } else if (ac->oc[1].m4ac.chan_config == 2) {
+ return NULL;
+ }
+ case 1:
+ if (!ac->tags_mapped && type == TYPE_SCE) {
+ ac->tags_mapped++;
+ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
+ }
+ default:
+ return NULL;
+ }
+}
+
+/**
+ * Decode an array of 4 bit element IDs, optionally interleaved with a
+ * stereo/mono switching bit.
+ *
+ * @param type speaker type/position for these channels
+ */
+static void decode_channel_map(uint8_t layout_map[][3],
+ enum ChannelPosition type,
+ GetBitContext *gb, int n)
+{
+ while (n--) {
+ enum RawDataBlockType syn_ele;
+ switch (type) {
+ case AAC_CHANNEL_FRONT:
+ case AAC_CHANNEL_BACK:
+ case AAC_CHANNEL_SIDE:
+ syn_ele = get_bits1(gb);
+ break;
+ case AAC_CHANNEL_CC:
+ skip_bits1(gb);
+ syn_ele = TYPE_CCE;
+ break;
+ case AAC_CHANNEL_LFE:
+ syn_ele = TYPE_LFE;
+ break;
+ default:
+ av_assert0(0);
+ }
+ layout_map[0][0] = syn_ele;
+ layout_map[0][1] = get_bits(gb, 4);
+ layout_map[0][2] = type;
+ layout_map++;
+ }
+}
+
+/**
+ * Decode program configuration element; reference: table 4.2.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
+ uint8_t (*layout_map)[3],
+ GetBitContext *gb)
+{
+ int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
+ int sampling_index;
+ int comment_len;
+ int tags;
+
+ skip_bits(gb, 2); // object_type
+
+ sampling_index = get_bits(gb, 4);
+ if (m4ac->sampling_index != sampling_index)
+ av_log(avctx, AV_LOG_WARNING,
+ "Sample rate index in program config element does not "
+ "match the sample rate index configured by the container.\n");
+
+ num_front = get_bits(gb, 4);
+ num_side = get_bits(gb, 4);
+ num_back = get_bits(gb, 4);
+ num_lfe = get_bits(gb, 2);
+ num_assoc_data = get_bits(gb, 3);
+ num_cc = get_bits(gb, 4);
+
+ if (get_bits1(gb))
+ skip_bits(gb, 4); // mono_mixdown_tag
+ if (get_bits1(gb))
+ skip_bits(gb, 4); // stereo_mixdown_tag
+
+ if (get_bits1(gb))
+ skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
+
+ if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
+ av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
+ return -1;
+ }
+ decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
+ tags = num_front;
+ decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
+ tags += num_side;
+ decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
+ tags += num_back;
+ decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
+ tags += num_lfe;
+
+ skip_bits_long(gb, 4 * num_assoc_data);
+
+ decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
+ tags += num_cc;
+
+ align_get_bits(gb);
+
+ /* comment field, first byte is length */
+ comment_len = get_bits(gb, 8) * 8;
+ if (get_bits_left(gb) < comment_len) {
+ av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
+ return AVERROR_INVALIDDATA;
+ }
+ skip_bits_long(gb, comment_len);
+ return tags;
+}
+
+/**
+ * Decode GA "General Audio" specific configuration; reference: table 4.1.
+ *
+ * @param ac pointer to AACContext, may be null
+ * @param avctx pointer to AVCCodecContext, used for logging
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
+ GetBitContext *gb,
+ MPEG4AudioConfig *m4ac,
+ int channel_config)
+{
+ int extension_flag, ret;
+ uint8_t layout_map[MAX_ELEM_ID*4][3];
+ int tags = 0;
+
+ if (get_bits1(gb)) { // frameLengthFlag
+ avpriv_request_sample(avctx, "960/120 MDCT window");
+ return AVERROR_PATCHWELCOME;
+ }
+
+ if (get_bits1(gb)) // dependsOnCoreCoder
+ skip_bits(gb, 14); // coreCoderDelay
+ extension_flag = get_bits1(gb);
+
+ if (m4ac->object_type == AOT_AAC_SCALABLE ||
+ m4ac->object_type == AOT_ER_AAC_SCALABLE)
+ skip_bits(gb, 3); // layerNr
+
+ if (channel_config == 0) {
+ skip_bits(gb, 4); // element_instance_tag
+ tags = decode_pce(avctx, m4ac, layout_map, gb);
+ if (tags < 0)
+ return tags;
+ } else {
+ if ((ret = set_default_channel_config(avctx, layout_map,
+ &tags, channel_config)))
+ return ret;
+ }
+
+ if (count_channels(layout_map, tags) > 1) {
+ m4ac->ps = 0;
+ } else if (m4ac->sbr == 1 && m4ac->ps == -1)
+ m4ac->ps = 1;
+
+ if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
+ return ret;
+
+ if (extension_flag) {
+ switch (m4ac->object_type) {
+ case AOT_ER_BSAC:
+ skip_bits(gb, 5); // numOfSubFrame
+ skip_bits(gb, 11); // layer_length
+ break;
+ case AOT_ER_AAC_LC:
+ case AOT_ER_AAC_LTP:
+ case AOT_ER_AAC_SCALABLE:
+ case AOT_ER_AAC_LD:
+ skip_bits(gb, 3); /* aacSectionDataResilienceFlag
+ * aacScalefactorDataResilienceFlag
+ * aacSpectralDataResilienceFlag
+ */
+ break;
+ }
+ skip_bits1(gb); // extensionFlag3 (TBD in version 3)
+ }
+ return 0;
+}
+
+/**
+ * Decode audio specific configuration; reference: table 1.13.
+ *
+ * @param ac pointer to AACContext, may be null
+ * @param avctx pointer to AVCCodecContext, used for logging
+ * @param m4ac pointer to MPEG4AudioConfig, used for parsing
+ * @param data pointer to buffer holding an audio specific config
+ * @param bit_size size of audio specific config or data in bits
+ * @param sync_extension look for an appended sync extension
+ *
+ * @return Returns error status or number of consumed bits. <0 - error
+ */
+static int decode_audio_specific_config(AACContext *ac,
+ AVCodecContext *avctx,
+ MPEG4AudioConfig *m4ac,
+ const uint8_t *data, int bit_size,
+ int sync_extension)
+{
+ GetBitContext gb;
+ int i, ret;
+
+ av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
+ for (i = 0; i < bit_size >> 3; i++)
+ av_dlog(avctx, "%02x ", data[i]);
+ av_dlog(avctx, "\n");
+
+ if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
+ return ret;
+
+ if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
+ sync_extension)) < 0)
+ return AVERROR_INVALIDDATA;
+ if (m4ac->sampling_index > 12) {
+ av_log(avctx, AV_LOG_ERROR,
+ "invalid sampling rate index %d\n",
+ m4ac->sampling_index);
+ return AVERROR_INVALIDDATA;
+ }
+
+ skip_bits_long(&gb, i);
+
+ switch (m4ac->object_type) {
+ case AOT_AAC_MAIN:
+ case AOT_AAC_LC:
+ case AOT_AAC_LTP:
+ if ((ret = decode_ga_specific_config(ac, avctx, &gb,
+ m4ac, m4ac->chan_config)) < 0)
+ return ret;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR,
+ "Audio object type %s%d is not supported.\n",
+ m4ac->sbr == 1 ? "SBR+" : "",
+ m4ac->object_type);
+ return AVERROR(ENOSYS);
+ }
+
+ av_dlog(avctx,
+ "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
+ m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
+ m4ac->sample_rate, m4ac->sbr,
+ m4ac->ps);
+
+ return get_bits_count(&gb);
+}
+
+/**
+ * linear congruential pseudorandom number generator
+ *
+ * @param previous_val pointer to the current state of the generator
+ *
+ * @return Returns a 32-bit pseudorandom integer
+ */
+static av_always_inline int lcg_random(unsigned previous_val)
+{
+ union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
+ return v.s;
+}
+
+static void reset_all_predictors(PredictorState *ps)
+{
+ int i;
+ for (i = 0; i < MAX_PREDICTORS; i++)
+ reset_predict_state(&ps[i]);
+}
+
+static int sample_rate_idx (int rate)
+{
+ if (92017 <= rate) return 0;
+ else if (75132 <= rate) return 1;
+ else if (55426 <= rate) return 2;
+ else if (46009 <= rate) return 3;
+ else if (37566 <= rate) return 4;
+ else if (27713 <= rate) return 5;
+ else if (23004 <= rate) return 6;
+ else if (18783 <= rate) return 7;
+ else if (13856 <= rate) return 8;
+ else if (11502 <= rate) return 9;
+ else if (9391 <= rate) return 10;
+ else return 11;
+}
+
+static void reset_predictor_group(PredictorState *ps, int group_num)
+{
+ int i;
+ for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
+ reset_predict_state(&ps[i]);
+}
+
+#define AAC_INIT_VLC_STATIC(num, size) \
+ INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
+ ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
+ sizeof(ff_aac_spectral_bits[num][0]), \
+ ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
+ sizeof(ff_aac_spectral_codes[num][0]), \
+ size);
+
+static void aacdec_init(AACContext *ac);
+
+static av_cold int aac_decode_init(AVCodecContext *avctx)
+{
+ AACContext *ac = avctx->priv_data;
+ int ret;
+
+ ac->avctx = avctx;
+ ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
+
+ aacdec_init(ac);
+#if CONFIG_AAC_FIXED
+ avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
+#else
+ avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
+#endif /* CONFIG_AAC_FIXED */
+
+ if (avctx->extradata_size > 0) {
+ if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
+ avctx->extradata,
+ avctx->extradata_size * 8,
+ 1)) < 0)
+ return ret;
+ } else {
+ int sr, i;
+ uint8_t layout_map[MAX_ELEM_ID*4][3];
+ int layout_map_tags;
+
+ sr = sample_rate_idx(avctx->sample_rate);
+ ac->oc[1].m4ac.sampling_index = sr;
+ ac->oc[1].m4ac.channels = avctx->channels;
+ ac->oc[1].m4ac.sbr = -1;
+ ac->oc[1].m4ac.ps = -1;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
+ if (ff_mpeg4audio_channels[i] == avctx->channels)
+ break;
+ if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
+ i = 0;
+ }
+ ac->oc[1].m4ac.chan_config = i;
+
+ if (ac->oc[1].m4ac.chan_config) {
+ int ret = set_default_channel_config(avctx, layout_map,
+ &layout_map_tags, ac->oc[1].m4ac.chan_config);
+ if (!ret)
+ output_configure(ac, layout_map, layout_map_tags,
+ OC_GLOBAL_HDR, 0);
+ else if (avctx->err_recognition & AV_EF_EXPLODE)
+ return AVERROR_INVALIDDATA;
+ }
+ }
+
+ if (avctx->channels > MAX_CHANNELS) {
+ av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ AAC_INIT_VLC_STATIC( 0, 304);
+ AAC_INIT_VLC_STATIC( 1, 270);
+ AAC_INIT_VLC_STATIC( 2, 550);
+ AAC_INIT_VLC_STATIC( 3, 300);
+ AAC_INIT_VLC_STATIC( 4, 328);
+ AAC_INIT_VLC_STATIC( 5, 294);
+ AAC_INIT_VLC_STATIC( 6, 306);
+ AAC_INIT_VLC_STATIC( 7, 268);
+ AAC_INIT_VLC_STATIC( 8, 510);
+ AAC_INIT_VLC_STATIC( 9, 366);
+ AAC_INIT_VLC_STATIC(10, 462);
+
+ AAC_RENAME(ff_aac_sbr_init)();
+
+ ff_fmt_convert_init(&ac->fmt_conv, avctx);
+
+#if CONFIG_AAC_FIXED
+ avpriv_fixed_dsp_init(&ac->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
+#else
+ avpriv_float_dsp_init(&ac->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
+#endif /* CONFIG_AAC_FIXED */
+
+ ac->random_state = 0x1f2e3d4c;
+
+ ff_aac_tableinit();
+
+ INIT_VLC_STATIC(&vlc_scalefactors, 7,
+ FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
+ ff_aac_scalefactor_bits,
+ sizeof(ff_aac_scalefactor_bits[0]),
+ sizeof(ff_aac_scalefactor_bits[0]),
+ ff_aac_scalefactor_code,
+ sizeof(ff_aac_scalefactor_code[0]),
+ sizeof(ff_aac_scalefactor_code[0]),
+ 352);
+
+ AAC_RENAME_32(ff_mdct_init)(&ac->mdct, 11, 1, 1.0 / RANGE15(1024.0));
+ AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small, 8, 1, 1.0 / RANGE15(128.0));
+ AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp, 11, 0, RANGE15(-2.0));
+ // window initialization
+ AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_1024), 4.0, 1024);
+ AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_128), 6.0, 128);
+ AAC_RENAME(ff_init_ff_sine_windows)(10);
+ AAC_RENAME(ff_init_ff_sine_windows)( 7);
+
+ AAC_RENAME(cbrt_tableinit)();
+
+ return 0;
+}
+
+/**
+ * Skip data_stream_element; reference: table 4.10.
+ */
+static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
+{
+ int byte_align = get_bits1(gb);
+ int count = get_bits(gb, 8);
+ if (count == 255)
+ count += get_bits(gb, 8);
+ if (byte_align)
+ align_get_bits(gb);
+
+ if (get_bits_left(gb) < 8 * count) {
+ av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
+ return AVERROR_INVALIDDATA;
+ }
+ skip_bits_long(gb, 8 * count);
+ return 0;
+}
+
+static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
+ GetBitContext *gb)
+{
+ int sfb;
+ if (get_bits1(gb)) {
+ ics->predictor_reset_group = get_bits(gb, 5);
+ if (ics->predictor_reset_group == 0 ||
+ ics->predictor_reset_group > 30) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Invalid Predictor Reset Group.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ }
+ for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
+ ics->prediction_used[sfb] = get_bits1(gb);
+ }
+ return 0;
+}
+
+/**
+ * Decode Long Term Prediction data; reference: table 4.xx.
+ */
+static void decode_ltp(LongTermPrediction *ltp,
+ GetBitContext *gb, uint8_t max_sfb)
+{
+ int sfb;
+
+ ltp->lag = get_bits(gb, 11);
+ ltp->coef = ltp_coef[get_bits(gb, 3)];
+ for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
+ ltp->used[sfb] = get_bits1(gb);
+}
+
+/**
+ * Decode Individual Channel Stream info; reference: table 4.6.
+ */
+static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
+ GetBitContext *gb)
+{
+ if (get_bits1(gb)) {
+ av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ ics->window_sequence[1] = ics->window_sequence[0];
+ ics->window_sequence[0] = get_bits(gb, 2);
+ ics->use_kb_window[1] = ics->use_kb_window[0];
+ ics->use_kb_window[0] = get_bits1(gb);
+ ics->num_window_groups = 1;
+ ics->group_len[0] = 1;
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ int i;
+ ics->max_sfb = get_bits(gb, 4);
+ for (i = 0; i < 7; i++) {
+ if (get_bits1(gb)) {
+ ics->group_len[ics->num_window_groups - 1]++;
+ } else {
+ ics->num_window_groups++;
+ ics->group_len[ics->num_window_groups - 1] = 1;
+ }
+ }
+ ics->num_windows = 8;
+ ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
+ ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
+ ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
+ ics->predictor_present = 0;
+ } else {
+ ics->max_sfb = get_bits(gb, 6);
+ ics->num_windows = 1;
+ ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
+ ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
+ ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
+ ics->predictor_present = get_bits1(gb);
+ ics->predictor_reset_group = 0;
+ if (ics->predictor_present) {
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
+ if (decode_prediction(ac, ics, gb)) {
+ goto fail;
+ }
+ } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Prediction is not allowed in AAC-LC.\n");
+ goto fail;
+ } else {
+ if ((ics->ltp.present = get_bits(gb, 1)))
+ decode_ltp(&ics->ltp, gb, ics->max_sfb);
+ }
+ }
+ }
+
+ if (ics->max_sfb > ics->num_swb) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Number of scalefactor bands in group (%d) "
+ "exceeds limit (%d).\n",
+ ics->max_sfb, ics->num_swb);
+ goto fail;
+ }
+
+ return 0;
+fail:
+ ics->max_sfb = 0;
+ return AVERROR_INVALIDDATA;
+}
+
+/**
+ * Decode band types (section_data payload); reference: table 4.46.
+ *
+ * @param band_type array of the used band type
+ * @param band_type_run_end array of the last scalefactor band of a band type run
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_band_types(AACContext *ac, enum BandType band_type[120],
+ int band_type_run_end[120], GetBitContext *gb,
+ IndividualChannelStream *ics)
+{
+ int g, idx = 0;
+ const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
+ for (g = 0; g < ics->num_window_groups; g++) {
+ int k = 0;
+ while (k < ics->max_sfb) {
+ uint8_t sect_end = k;
+ int sect_len_incr;
+ int sect_band_type = get_bits(gb, 4);
+ if (sect_band_type == 12) {
+ av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
+ return AVERROR_INVALIDDATA;
+ }
+ do {
+ sect_len_incr = get_bits(gb, bits);
+ sect_end += sect_len_incr;
+ if (get_bits_left(gb) < 0) {
+ av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
+ return AVERROR_INVALIDDATA;
+ }
+ if (sect_end > ics->max_sfb) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Number of bands (%d) exceeds limit (%d).\n",
+ sect_end, ics->max_sfb);
+ return AVERROR_INVALIDDATA;
+ }
+ } while (sect_len_incr == (1 << bits) - 1);
+ for (; k < sect_end; k++) {
+ band_type [idx] = sect_band_type;
+ band_type_run_end[idx++] = sect_end;
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Decode scalefactors; reference: table 4.47.
+ *
+ * @param global_gain first scalefactor value as scalefactors are differentially coded
+ * @param band_type array of the used band type
+ * @param band_type_run_end array of the last scalefactor band of a band type run
+ * @param sf array of scalefactors or intensity stereo positions
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
+ unsigned int global_gain,
+ IndividualChannelStream *ics,
+ enum BandType band_type[120],
+ int band_type_run_end[120])
+{
+ int g, i, idx = 0;
+ int offset[3] = { global_gain, global_gain - 90, 0 };
+ int clipped_offset;
+ int noise_flag = 1;
+ for (g = 0; g < ics->num_window_groups; g++) {
+ for (i = 0; i < ics->max_sfb;) {
+ int run_end = band_type_run_end[idx];
+ if (band_type[idx] == ZERO_BT) {
+ for (; i < run_end; i++, idx++)
+ sf[idx] = FIXR(0.);
+ } else if ((band_type[idx] == INTENSITY_BT) ||
+ (band_type[idx] == INTENSITY_BT2)) {
+ for (; i < run_end; i++, idx++) {
+ offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+ clipped_offset = av_clip(offset[2], -155, 100);
+ if (offset[2] != clipped_offset) {
+ avpriv_request_sample(ac->avctx,
+ "If you heard an audible artifact, there may be a bug in the decoder. "
+ "Clipped intensity stereo position (%d -> %d)",
+ offset[2], clipped_offset);
+ }
+#if CONFIG_AAC_FIXED
+ sf[idx] = 100 - clipped_offset;
+#else
+ sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
+#endif /* CONFIG_AAC_FIXED */
+ }
+ } else if (band_type[idx] == NOISE_BT) {
+ for (; i < run_end; i++, idx++) {
+ if (noise_flag-- > 0)
+ offset[1] += get_bits(gb, 9) - 256;
+ else
+ offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+ clipped_offset = av_clip(offset[1], -100, 155);
+ if (offset[1] != clipped_offset) {
+ avpriv_request_sample(ac->avctx,
+ "If you heard an audible artifact, there may be a bug in the decoder. "
+ "Clipped noise gain (%d -> %d)",
+ offset[1], clipped_offset);
+ }
+#if CONFIG_AAC_FIXED
+ sf[idx] = -(100 + clipped_offset);
+#else
+ sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
+#endif /* CONFIG_AAC_FIXED */
+ }
+ } else {
+ for (; i < run_end; i++, idx++) {
+ offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+ if (offset[0] > 255U) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Scalefactor (%d) out of range.\n", offset[0]);
+ return AVERROR_INVALIDDATA;
+ }
+#if CONFIG_AAC_FIXED
+ sf[idx] = -offset[0];
+#else
+ sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
+#endif /* CONFIG_AAC_FIXED */
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Decode pulse data; reference: table 4.7.
+ */
+static int decode_pulses(Pulse *pulse, GetBitContext *gb,
+ const uint16_t *swb_offset, int num_swb)
+{
+ int i, pulse_swb;
+ pulse->num_pulse = get_bits(gb, 2) + 1;
+ pulse_swb = get_bits(gb, 6);
+ if (pulse_swb >= num_swb)
+ return -1;
+ pulse->pos[0] = swb_offset[pulse_swb];
+ pulse->pos[0] += get_bits(gb, 5);
+ if (pulse->pos[0] > 1023)
+ return -1;
+ pulse->amp[0] = get_bits(gb, 4);
+ for (i = 1; i < pulse->num_pulse; i++) {
+ pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
+ if (pulse->pos[i] > 1023)
+ return -1;
+ pulse->amp[i] = get_bits(gb, 4);
+ }
+ return 0;
+}
+
+/**
+ * Decode Temporal Noise Shaping data; reference: table 4.48.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
+ GetBitContext *gb, const IndividualChannelStream *ics)
+{
+ int w, filt, i, coef_len, coef_res, coef_compress;
+ const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
+ const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
+ for (w = 0; w < ics->num_windows; w++) {
+ if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
+ coef_res = get_bits1(gb);
+
+ for (filt = 0; filt < tns->n_filt[w]; filt++) {
+ int tmp2_idx;
+ tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
+
+ if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "TNS filter order %d is greater than maximum %d.\n",
+ tns->order[w][filt], tns_max_order);
+ tns->order[w][filt] = 0;
+ return AVERROR_INVALIDDATA;
+ }
+ if (tns->order[w][filt]) {
+ tns->direction[w][filt] = get_bits1(gb);
+ coef_compress = get_bits1(gb);
+ coef_len = coef_res + 3 - coef_compress;
+ tmp2_idx = 2 * coef_compress + coef_res;
+
+ for (i = 0; i < tns->order[w][filt]; i++)
+ tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Decode Mid/Side data; reference: table 4.54.
+ *
+ * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
+ * [1] mask is decoded from bitstream; [2] mask is all 1s;
+ * [3] reserved for scalable AAC
+ */
+static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
+ int ms_present)
+{
+ int idx;
+ if (ms_present == 1) {
+ for (idx = 0;
+ idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
+ idx++)
+ cpe->ms_mask[idx] = get_bits1(gb);
+ } else if (ms_present == 2) {
+ memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask[0]) * cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb);
+ }
+}
+
+/**
+ * Decode spectral data; reference: table 4.50.
+ * Dequantize and scale spectral data; reference: 4.6.3.3.
+ *
+ * @param coef array of dequantized, scaled spectral data
+ * @param sf array of scalefactors or intensity stereo positions
+ * @param pulse_present set if pulses are present
+ * @param pulse pointer to pulse data struct
+ * @param band_type array of the used band type
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
+ GetBitContext *gb, const INTFLOAT sf[120],
+ int pulse_present, const Pulse *pulse,
+ const IndividualChannelStream *ics,
+ enum BandType band_type[120])
+{
+ int i, k, g, idx = 0;
+ const int c = 1024 / ics->num_windows;
+ const uint16_t *offsets = ics->swb_offset;
+ INTFLOAT *coef_base = coef;
+
+ for (g = 0; g < ics->num_windows; g++)
+ memset(coef + g * 128 + offsets[ics->max_sfb], 0,
+ sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
+
+ for (g = 0; g < ics->num_window_groups; g++) {
+ unsigned g_len = ics->group_len[g];
+
+ for (i = 0; i < ics->max_sfb; i++, idx++) {
+ const unsigned cbt_m1 = band_type[idx] - 1;
+ INTFLOAT *cfo = coef + offsets[i];
+ int off_len = offsets[i + 1] - offsets[i];
+ int group;
+
+ if (cbt_m1 >= INTENSITY_BT2 - 1) {
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+ memset(cfo, 0, off_len * sizeof(INTFLOAT));
+ }
+ } else if (cbt_m1 == NOISE_BT - 1) {
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+#if !CONFIG_AAC_FIXED
+ float scale;
+#endif /* !CONFIG_AAC_FIXED */
+ INTFLOAT band_energy;
+
+ for (k = 0; k < off_len; k++) {
+ ac->random_state = lcg_random(ac->random_state);
+#if CONFIG_AAC_FIXED
+ cfo[k] = ac->random_state >> 3;
+
+#else
+ cfo[k] = ac->random_state;
+#endif /* CONFIG_AAC_FIXED */
+ }
+
+ band_energy = ac->fdsp.AAC_RENAME2(scalarproduct)(cfo, cfo, off_len);
+#if CONFIG_AAC_FIXED
+ band_energy = fixed_sqrt(band_energy, 31);
+ noise_scale(cfo, sf[idx], band_energy, off_len);
+#else
+ scale = sf[idx] / sqrtf(band_energy);
+ ac->fdsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
+#endif /* CONFIG_AAC_FIXED */
+ }
+ } else {
+#if !CONFIG_AAC_FIXED
+ const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
+#endif /* !CONFIG_AAC_FIXED */
+ const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
+ VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
+ OPEN_READER(re, gb);
+
+ switch (cbt_m1 >> 1) {
+ case 0:
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+ INTFLOAT *cf = cfo;
+ int len = off_len;
+
+ do {
+ int code;
+ unsigned cb_idx;
+
+ UPDATE_CACHE(re, gb);
+ GET_VLC(code, re, gb, vlc_tab, 8, 2);
+ cb_idx = cb_vector_idx[code];
+#if CONFIG_AAC_FIXED
+ cf = DEC_SQUAD(cf, cb_idx);
+#else
+ cf = VMUL4(cf, vq, cb_idx, sf + idx);
+#endif /* CONFIG_AAC_FIXED */
+ } while (len -= 4);
+ }
+ break;
+
+ case 1:
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+ INTFLOAT *cf = cfo;
+ int len = off_len;
+
+ do {
+ int code;
+ unsigned nnz;
+ unsigned cb_idx;
+ uint32_t bits;
+
+ UPDATE_CACHE(re, gb);
+ GET_VLC(code, re, gb, vlc_tab, 8, 2);
+ cb_idx = cb_vector_idx[code];
+ nnz = cb_idx >> 8 & 15;
+ bits = nnz ? GET_CACHE(re, gb) : 0;
+ LAST_SKIP_BITS(re, gb, nnz);
+#if CONFIG_AAC_FIXED
+ cf = DEC_UQUAD(cf, cb_idx, bits);
+#else
+ cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
+#endif /* CONFIG_AAC_FIXED */
+ } while (len -= 4);
+ }
+ break;
+
+ case 2:
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+ INTFLOAT *cf = cfo;
+ int len = off_len;
+
+ do {
+ int code;
+ unsigned cb_idx;
+
+ UPDATE_CACHE(re, gb);
+ GET_VLC(code, re, gb, vlc_tab, 8, 2);
+ cb_idx = cb_vector_idx[code];
+#if CONFIG_AAC_FIXED
+ cf = DEC_SPAIR(cf, cb_idx);
+#else
+ cf = VMUL2(cf, vq, cb_idx, sf + idx);
+#endif /* CONFIG_AAC_FIXED */
+ } while (len -= 2);
+ }
+ break;
+
+ case 3:
+ case 4:
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+ INTFLOAT *cf = cfo;
+ int len = off_len;
+
+ do {
+ int code;
+ unsigned nnz;
+ unsigned cb_idx;
+ unsigned sign;
+
+ UPDATE_CACHE(re, gb);
+ GET_VLC(code, re, gb, vlc_tab, 8, 2);
+ cb_idx = cb_vector_idx[code];
+ nnz = cb_idx >> 8 & 15;
+ sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
+ LAST_SKIP_BITS(re, gb, nnz);
+#if CONFIG_AAC_FIXED
+ cf = DEC_UPAIR(cf, cb_idx, sign);
+#else
+ cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
+#endif /* CONFIG_AAC_FIXED */
+ } while (len -= 2);
+ }
+ break;
+
+ default:
+ for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
+#if CONFIG_AAC_FIXED
+ int *icf = cfo;
+ int v;
+#else
+ float *cf = cfo;
+ uint32_t *icf = (uint32_t *) cf;
+#endif /* CONFIG_AAC_FIXED */
+ int len = off_len;
+
+ do {
+ int code;
+ unsigned nzt, nnz;
+ unsigned cb_idx;
+ uint32_t bits;
+ int j;
+
+ UPDATE_CACHE(re, gb);
+ GET_VLC(code, re, gb, vlc_tab, 8, 2);
+
+ if (!code) {
+ *icf++ = 0;
+ *icf++ = 0;
+ continue;
+ }
+
+ cb_idx = cb_vector_idx[code];
+ nnz = cb_idx >> 12;
+ nzt = cb_idx >> 8;
+ bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
+ LAST_SKIP_BITS(re, gb, nnz);
+
+ for (j = 0; j < 2; j++) {
+ if (nzt & 1<<j) {
+ uint32_t b;
+ int n;
+ /* The total length of escape_sequence must be < 22 bits according
+ to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
+ UPDATE_CACHE(re, gb);
+ b = GET_CACHE(re, gb);
+ b = 31 - av_log2(~b);
+
+ if (b > 8) {
+ av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ SKIP_BITS(re, gb, b + 1);
+ b += 4;
+ n = (1 << b) + SHOW_UBITS(re, gb, b);
+ LAST_SKIP_BITS(re, gb, b);
+#if CONFIG_AAC_FIXED
+ v = n;
+ if (bits & 1U<<31)
+ v = -v;
+ *icf++ = v;
+#else
+ *icf++ = cbrt_tab[n] | (bits & 1U<<31);
+#endif /* CONFIG_AAC_FIXED */
+ bits <<= 1;
+ } else {
+#if CONFIG_AAC_FIXED
+ v = cb_idx & 15;
+ if (bits & 1U<<31)
+ v = -v;
+ *icf++ = v;
+#else
+ unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
+ *icf++ = (bits & 1U<<31) | v;
+#endif /* CONFIG_AAC_FIXED */
+ bits <<= !!v;
+ }
+ cb_idx >>= 4;
+ }
+ } while (len -= 2);
+#if !CONFIG_AAC_FIXED
+ ac->fdsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
+#endif /* !CONFIG_AAC_FIXED */
+ }
+ }
+
+ CLOSE_READER(re, gb);
+ }
+ }
+ coef += g_len << 7;
+ }
+
+ if (pulse_present) {
+ idx = 0;
+ for (i = 0; i < pulse->num_pulse; i++) {
+ INTFLOAT co = coef_base[ pulse->pos[i] ];
+ while (offsets[idx + 1] <= pulse->pos[i])
+ idx++;
+ if (band_type[idx] != NOISE_BT && sf[idx]) {
+ INTFLOAT ico = -pulse->amp[i];
+#if CONFIG_AAC_FIXED
+ if (co) {
+ ico = co + (co > 0 ? -ico : ico);
+ }
+ coef_base[ pulse->pos[i] ] = ico;
+#else
+ if (co) {
+ co /= sf[idx];
+ ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
+ }
+ coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
+#endif /* CONFIG_AAC_FIXED */
+ }
+ }
+ }
+#if CONFIG_AAC_FIXED
+ coef = coef_base;
+ idx = 0;
+ for (g = 0; g < ics->num_window_groups; g++) {
+ unsigned g_len = ics->group_len[g];
+
+ for (i = 0; i < ics->max_sfb; i++, idx++) {
+ const unsigned cbt_m1 = band_type[idx] - 1;
+ int *cfo = coef + offsets[i];
+ int off_len = offsets[i + 1] - offsets[i];
+ int group;
+
+ if (cbt_m1 < NOISE_BT - 1) {
+ for (group = 0; group < (int)g_len; group++, cfo+=128) {
+ ac->vector_pow43(cfo, off_len);
+ ac->subband_scale(cfo, cfo, sf[idx], 34, off_len);
+ }
+ }
+ }
+ coef += g_len << 7;
+ }
+#endif /* CONFIG_AAC_FIXED */
+ return 0;
+}
+
+/**
+ * Apply AAC-Main style frequency domain prediction.
+ */
+static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
+{
+ int sfb, k;
+
+ if (!sce->ics.predictor_initialized) {
+ reset_all_predictors(sce->predictor_state);
+ sce->ics.predictor_initialized = 1;
+ }
+
+ if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
+ for (sfb = 0;
+ sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
+ sfb++) {
+ for (k = sce->ics.swb_offset[sfb];
+ k < sce->ics.swb_offset[sfb + 1];
+ k++) {
+ predict(&sce->predictor_state[k], &sce->coeffs[k],
+ sce->ics.predictor_present &&
+ sce->ics.prediction_used[sfb]);
+ }
+ }
+ if (sce->ics.predictor_reset_group)
+ reset_predictor_group(sce->predictor_state,
+ sce->ics.predictor_reset_group);
+ } else
+ reset_all_predictors(sce->predictor_state);
+}
+
+/**
+ * Decode an individual_channel_stream payload; reference: table 4.44.
+ *
+ * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
+ * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_ics(AACContext *ac, SingleChannelElement *sce,
+ GetBitContext *gb, int common_window, int scale_flag)
+{
+ Pulse pulse;
+ TemporalNoiseShaping *tns = &sce->tns;
+ IndividualChannelStream *ics = &sce->ics;
+ INTFLOAT *out = sce->coeffs;
+ int global_gain, pulse_present = 0;
+ int ret;
+
+ /* This assignment is to silence a GCC warning about the variable being used
+ * uninitialized when in fact it always is.
+ */
+ pulse.num_pulse = 0;
+
+ global_gain = get_bits(gb, 8);
+
+ if (!common_window && !scale_flag) {
+ if (decode_ics_info(ac, ics, gb) < 0)
+ return AVERROR_INVALIDDATA;
+ }
+
+ if ((ret = decode_band_types(ac, sce->band_type,
+ sce->band_type_run_end, gb, ics)) < 0)
+ return ret;
+ if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
+ sce->band_type, sce->band_type_run_end)) < 0)
+ return ret;
+
+ pulse_present = 0;
+ if (!scale_flag) {
+ if ((pulse_present = get_bits1(gb))) {
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Pulse tool not allowed in eight short sequence.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
+ av_log(ac->avctx, AV_LOG_ERROR,
+ "Pulse data corrupt or invalid.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ }
+ if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
+ return AVERROR_INVALIDDATA;
+ if (get_bits1(gb)) {
+ avpriv_request_sample(ac->avctx, "SSR");
+ return AVERROR_PATCHWELCOME;
+ }
+ }
+
+ if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
+ &pulse, ics, sce->band_type) < 0)
+ return AVERROR_INVALIDDATA;
+
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
+ apply_prediction(ac, sce);
+
+ return 0;
+}
+
+/**
+ * Mid/Side stereo decoding; reference: 4.6.8.1.3.
+ */
+static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
+{
+ const IndividualChannelStream *ics = &cpe->ch[0].ics;
+ INTFLOAT *ch0 = cpe->ch[0].coeffs;
+ INTFLOAT *ch1 = cpe->ch[1].coeffs;
+ int g, i, group, idx = 0;
+ const uint16_t *offsets = ics->swb_offset;
+ for (g = 0; g < ics->num_window_groups; g++) {
+ for (i = 0; i < ics->max_sfb; i++, idx++) {
+ if (cpe->ms_mask[idx] &&
+ cpe->ch[0].band_type[idx] < NOISE_BT &&
+ cpe->ch[1].band_type[idx] < NOISE_BT) {
+ for (group = 0; group < ics->group_len[g]; group++) {
+ ac->fdsp.AAC_RENAME2(butterflies)(ch0 + group * 128 + offsets[i],
+ ch1 + group * 128 + offsets[i],
+ offsets[i+1] - offsets[i]);
+ }
+ }
+ }
+ ch0 += ics->group_len[g] * 128;
+ ch1 += ics->group_len[g] * 128;
+ }
+}
+
+/**
+ * intensity stereo decoding; reference: 4.6.8.2.3
+ *
+ * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
+ * [1] mask is decoded from bitstream; [2] mask is all 1s;
+ * [3] reserved for scalable AAC
+ */
+static void apply_intensity_stereo(AACContext *ac,
+ ChannelElement *cpe, int ms_present)
+{
+ const IndividualChannelStream *ics = &cpe->ch[1].ics;
+ SingleChannelElement *sce1 = &cpe->ch[1];
+ INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
+ const uint16_t *offsets = ics->swb_offset;
+ int g, group, i, idx = 0;
+ int c;
+ INTFLOAT scale;
+ for (g = 0; g < ics->num_window_groups; g++) {
+ for (i = 0; i < ics->max_sfb;) {
+ if (sce1->band_type[idx] == INTENSITY_BT ||
+ sce1->band_type[idx] == INTENSITY_BT2) {
+ const int bt_run_end = sce1->band_type_run_end[idx];
+ for (; i < bt_run_end; i++, idx++) {
+ c = -1 + 2 * (sce1->band_type[idx] - 14);
+ if (ms_present)
+ c *= 1 - 2 * cpe->ms_mask[idx];
+ scale = c * sce1->sf[idx];
+ for (group = 0; group < ics->group_len[g]; group++)
+#if CONFIG_AAC_FIXED
+ ac->subband_scale(coef1 + group * 128 + offsets[i],
+ coef0 + group * 128 + offsets[i],
+ scale,
+ 23,
+ offsets[i + 1] - offsets[i]);
+#else
+ ac->fdsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
+ coef0 + group * 128 + offsets[i],
+ scale,
+ offsets[i + 1] - offsets[i]);
+#endif /* CONFIG_AAC_FIXED */
+ }
+ } else {
+ int bt_run_end = sce1->band_type_run_end[idx];
+ idx += bt_run_end - i;
+ i = bt_run_end;
+ }
+ }
+ coef0 += ics->group_len[g] * 128;
+ coef1 += ics->group_len[g] * 128;
+ }
+}
+
+/**
+ * Decode a channel_pair_element; reference: table 4.4.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
+{
+ int i, ret, common_window, ms_present = 0;
+
+ common_window = get_bits1(gb);
+ if (common_window) {
+ if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
+ return AVERROR_INVALIDDATA;
+ i = cpe->ch[1].ics.use_kb_window[0];
+ cpe->ch[1].ics = cpe->ch[0].ics;
+ cpe->ch[1].ics.use_kb_window[1] = i;
+ if (cpe->ch[1].ics.predictor_present &&
+ (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
+ if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
+ decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
+ ms_present = get_bits(gb, 2);
+ if (ms_present == 3) {
+ av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
+ return AVERROR_INVALIDDATA;
+ } else if (ms_present)
+ decode_mid_side_stereo(cpe, gb, ms_present);
+ }
+ if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
+ return ret;
+ if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
+ return ret;
+
+ if (common_window) {
+ if (ms_present)
+ apply_mid_side_stereo(ac, cpe);
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
+ apply_prediction(ac, &cpe->ch[0]);
+ apply_prediction(ac, &cpe->ch[1]);
+ }
+ }
+
+ apply_intensity_stereo(ac, cpe, ms_present);
+ return 0;
+}
+
+/**
+ * Decode coupling_channel_element; reference: table 4.8.
+ *
+ * @return Returns error status. 0 - OK, !0 - error
+ */
+static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
+{
+ int num_gain = 0;
+ int c, g, sfb, ret;
+ int sign;
+ INTFLOAT scale;
+ SingleChannelElement *sce = &che->ch[0];
+ ChannelCoupling *coup = &che->coup;
+
+ coup->coupling_point = 2 * get_bits1(gb);
+ coup->num_coupled = get_bits(gb, 3);
+ for (c = 0; c <= coup->num_coupled; c++) {
+ num_gain++;
+ coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
+ coup->id_select[c] = get_bits(gb, 4);
+ if (coup->type[c] == TYPE_CPE) {
+ coup->ch_select[c] = get_bits(gb, 2);
+ if (coup->ch_select[c] == 3)
+ num_gain++;
+ } else
+ coup->ch_select[c] = 2;
+ }
+ coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
+
+ sign = get_bits(gb, 1);
+ scale = AAC_RENAME(cce_scale)[get_bits(gb, 2)];
+
+ if ((ret = decode_ics(ac, sce, gb, 0, 0)))
+ return ret;
+
+ for (c = 0; c < num_gain; c++) {
+ int idx = 0;
+ int cge = 1;
+ int gain = 0;
+ INTFLOAT gain_cache = FIXR10(1.);
+ if (c) {
+ cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
+ gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
+ gain_cache = GET_GAIN(scale, gain);
+ }
+ if (coup->coupling_point == AFTER_IMDCT) {
+ coup->gain[c][0] = gain_cache;
+ } else {
+ for (g = 0; g < sce->ics.num_window_groups; g++) {
+ for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
+ if (sce->band_type[idx] != ZERO_BT) {
+ if (!cge) {
+ int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+ if (t) {
+ int s = 1;
+ t = gain += t;
+ if (sign) {
+ s -= 2 * (t & 0x1);
+ t >>= 1;
+ }
+ gain_cache = GET_GAIN(scale, t) * s;
+ }
+ }
+ coup->gain[c][idx] = gain_cache;
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
+ *
+ * @return Returns number of bytes consumed.
+ */
+static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
+ GetBitContext *gb)
+{
+ int i;
+ int num_excl_chan = 0;
+
+ do {
+ for (i = 0; i < 7; i++)
+ che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
+ } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
+
+ return num_excl_chan / 7;
+}
+
+/**
+ * Decode dynamic range information; reference: table 4.52.
+ *
+ * @return Returns number of bytes consumed.
+ */
+static int decode_dynamic_range(DynamicRangeControl *che_drc,
+ GetBitContext *gb)
+{
+ int n = 1;
+ int drc_num_bands = 1;
+ int i;
+
+ /* pce_tag_present? */
+ if (get_bits1(gb)) {
+ che_drc->pce_instance_tag = get_bits(gb, 4);
+ skip_bits(gb, 4); // tag_reserved_bits
+ n++;
+ }
+
+ /* excluded_chns_present? */
+ if (get_bits1(gb)) {
+ n += decode_drc_channel_exclusions(che_drc, gb);
+ }
+
+ /* drc_bands_present? */
+ if (get_bits1(gb)) {
+ che_drc->band_incr = get_bits(gb, 4);
+ che_drc->interpolation_scheme = get_bits(gb, 4);
+ n++;
+ drc_num_bands += che_drc->band_incr;
+ for (i = 0; i < drc_num_bands; i++) {
+ che_drc->band_top[i] = get_bits(gb, 8);
+ n++;
+ }
+ }
+
+ /* prog_ref_level_present? */
+ if (get_bits1(gb)) {
+ che_drc->prog_ref_level = get_bits(gb, 7);
+ skip_bits1(gb); // prog_ref_level_reserved_bits
+ n++;
+ }
+
+ for (i = 0; i < drc_num_bands; i++) {
+ che_drc->dyn_rng_sgn[i] = get_bits1(gb);
+ che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
+ n++;
+ }
+
+ return n;
+}
+
+static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
+ uint8_t buf[256];
+ int i, major, minor;
+
+ if (len < 13+7*8)
+ goto unknown;
+
+ get_bits(gb, 13); len -= 13;
+
+ for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
+ buf[i] = get_bits(gb, 8);
+
+ buf[i] = 0;
+ if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
+ av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
+
+ if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
+ ac->avctx->internal->skip_samples = 1024;
+ }
+
+unknown:
+ skip_bits_long(gb, len);
+
+ return 0;
+}
+
+/**
+ * Decode extension data (incomplete); reference: table 4.51.
+ *
+ * @param cnt length of TYPE_FIL syntactic element in bytes
+ *
+ * @return Returns number of bytes consumed
+ */
+static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
+ ChannelElement *che, enum RawDataBlockType elem_type)
+{
+ int crc_flag = 0;
+ int res = cnt;
+ switch (get_bits(gb, 4)) { // extension type
+ case EXT_SBR_DATA_CRC:
+ crc_flag++;
+ case EXT_SBR_DATA:
+ if (!che) {
+ av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
+ return res;
+ } else if (!ac->oc[1].m4ac.sbr) {
+ av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
+ skip_bits_long(gb, 8 * cnt - 4);
+ return res;
+ } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
+ av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
+ skip_bits_long(gb, 8 * cnt - 4);
+ return res;
+ } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
+ ac->oc[1].m4ac.sbr = 1;
+ ac->oc[1].m4ac.ps = 1;
+ output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
+ ac->oc[1].status, 1);
+ } else {
+ ac->oc[1].m4ac.sbr = 1;
+ }
+ res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
+ break;
+ case EXT_DYNAMIC_RANGE:
+ res = decode_dynamic_range(&ac->che_drc, gb);
+ break;
+ case EXT_FILL:
+ decode_fill(ac, gb, 8 * cnt - 4);
+ break;
+ case EXT_FILL_DATA:
+ case EXT_DATA_ELEMENT:
+ default:
+ skip_bits_long(gb, 8 * cnt - 4);
+ break;
+ };
+ return res;
+}
+
+/**
+ * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
+ *
+ * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
+ * @param coef spectral coefficients
+ */
+static void apply_tns(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
+ IndividualChannelStream *ics, int decode)
+{
+ const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
+ int w, filt, m, i;
+ int bottom, top, order, start, end, size, inc;
+ INTFLOAT lpc[TNS_MAX_ORDER];
+ INTFLOAT tmp[TNS_MAX_ORDER+1];
+
+ for (w = 0; w < ics->num_windows; w++) {
+ bottom = ics->num_swb;
+ for (filt = 0; filt < tns->n_filt[w]; filt++) {
+ top = bottom;
+ bottom = FFMAX(0, top - tns->length[w][filt]);
+ order = tns->order[w][filt];
+ if (order == 0)
+ continue;
+
+ // tns_decode_coef
+ AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
+
+ start = ics->swb_offset[FFMIN(bottom, mmm)];
+ end = ics->swb_offset[FFMIN( top, mmm)];
+ if ((size = end - start) <= 0)
+ continue;
+ if (tns->direction[w][filt]) {
+ inc = -1;
+ start = end - 1;
+ } else {
+ inc = 1;
+ }
+ start += w * 128;
+
+ if (decode) {
+ // ar filter
+ for (m = 0; m < size; m++, start += inc)
+ for (i = 1; i <= FFMIN(m, order); i++)
+ coef[start] -= AAC_MUL26(coef[start - i * inc], lpc[i - 1]);
+ } else {
+ // ma filter
+ for (m = 0; m < size; m++, start += inc) {
+ tmp[0] = coef[start];
+ for (i = 1; i <= FFMIN(m, order); i++)
+ coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
+ for (i = order; i > 0; i--)
+ tmp[i] = tmp[i - 1];
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Apply windowing and MDCT to obtain the spectral
+ * coefficient from the predicted sample by LTP.
+ */
+static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
+ INTFLOAT *in, IndividualChannelStream *ics)
+{
+ const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
+ const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
+ const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
+ const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
+
+ if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
+ ac->fdsp.AAC_RENAME(vector_fmul)(in, in, lwindow_prev, 1024);
+ } else {
+ memset(in, 0, 448 * sizeof(INTFLOAT));
+ ac->fdsp.AAC_RENAME(vector_fmul)(in + 448, in + 448, swindow_prev, 128);
+ }
+ if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
+ ac->fdsp.AAC_RENAME(vector_fmul_reverse)(in + 1024, in + 1024, lwindow, 1024);
+ } else {
+ ac->fdsp.AAC_RENAME(vector_fmul_reverse)(in + 1024 + 448, in + 1024 + 448, swindow, 128);
+ memset(in + 1024 + 576, 0, 448 * sizeof(INTFLOAT));
+ }
+ ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
+}
+
+/**
+ * Apply the long term prediction
+ */
+static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
+{
+ const LongTermPrediction *ltp = &sce->ics.ltp;
+ const uint16_t *offsets = sce->ics.swb_offset;
+ int i, sfb;
+
+ if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
+ INTFLOAT *predTime = sce->ret;
+ INTFLOAT *predFreq = ac->buf_mdct;
+ int16_t num_samples = 2048;
+
+ if (ltp->lag < 1024)
+ num_samples = ltp->lag + 1024;
+ for (i = 0; i < num_samples; i++)
+ predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
+ memset(&predTime[i], 0, (2048 - i) * sizeof(INTFLOAT));
+
+ ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
+
+ if (sce->tns.present)
+ ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
+
+ for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
+ if (ltp->used[sfb])
+ for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
+ sce->coeffs[i] += predFreq[i];
+ }
+}
+
+/**
+ * Update the LTP buffer for next frame
+ */
+static void update_ltp(AACContext *ac, SingleChannelElement *sce)
+{
+ IndividualChannelStream *ics = &sce->ics;
+ INTFLOAT *saved = sce->saved;
+ INTFLOAT *saved_ltp = sce->coeffs;
+ const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
+ const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
+ int i;
+
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ memcpy(saved_ltp, saved, 512 * sizeof(INTFLOAT));
+ memset(saved_ltp + 576, 0, 448 * sizeof(INTFLOAT));
+ ac->fdsp.AAC_RENAME(vector_fmul_reverse)(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
+ for (i = 0; i < 64; i++)
+ saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
+ } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
+ memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(INTFLOAT));
+ memset(saved_ltp + 576, 0, 448 * sizeof(INTFLOAT));
+ ac->fdsp.AAC_RENAME(vector_fmul_reverse)(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
+ for (i = 0; i < 64; i++)
+ saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
+ } else { // LONG_STOP or ONLY_LONG
+ ac->fdsp.AAC_RENAME(vector_fmul_reverse)(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
+ for (i = 0; i < 512; i++)
+ saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
+ }
+
+ memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
+ memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
+ memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
+}
+
+/**
+ * Conduct IMDCT and windowing.
+ */
+static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
+{
+ IndividualChannelStream *ics = &sce->ics;
+ INTFLOAT *in = sce->coeffs;
+ INTFLOAT *out = sce->ret;
+ INTFLOAT *saved = sce->saved;
+ const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
+ const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
+ const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
+ INTFLOAT *buf = ac->buf_mdct;
+ INTFLOAT *temp = ac->temp;
+ int i;
+
+ // imdct
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ for (i = 0; i < 1024; i += 128)
+ ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
+ } else {
+ ac->mdct.imdct_half(&ac->mdct, buf, in);
+#if CONFIG_AAC_FIXED
+ for (i=0; i<1024; i++)
+ buf[i] = (buf[i] + 4) >> 3;
+#endif /* CONFIG_AAC_FIXED */
+ }
+
+ /* window overlapping
+ * NOTE: To simplify the overlapping code, all 'meaningless' short to long
+ * and long to short transitions are considered to be short to short
+ * transitions. This leaves just two cases (long to long and short to short)
+ * with a little special sauce for EIGHT_SHORT_SEQUENCE.
+ */
+ if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
+ (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
+ ac->fdsp.AAC_RENAME(vector_fmul_window)( out, saved, buf, lwindow_prev, 512);
+ } else {
+ memcpy( out, saved, 448 * sizeof(INTFLOAT));
+
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
+ memcpy( out + 448 + 4*128, temp, 64 * sizeof(INTFLOAT));
+ } else {
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(out + 448, saved + 448, buf, swindow_prev, 64);
+ memcpy( out + 576, buf + 64, 448 * sizeof(INTFLOAT));
+ }
+ }
+
+ // buffer update
+ if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
+ memcpy( saved, temp + 64, 64 * sizeof(INTFLOAT));
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
+ ac->fdsp.AAC_RENAME(vector_fmul_window)(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
+ memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(INTFLOAT));
+ } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
+ memcpy( saved, buf + 512, 448 * sizeof(INTFLOAT));
+ memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(INTFLOAT));
+ } else { // LONG_STOP or ONLY_LONG
+ memcpy( saved, buf + 512, 512 * sizeof(INTFLOAT));
+ }
+}
+
+/**
+ * channel coupling transformation interface
+ *
+ * @param apply_coupling_method pointer to (in)dependent coupling function
+ */
+static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
+ enum RawDataBlockType type, int elem_id,
+ enum CouplingPoint coupling_point,
+ void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
+{
+ int i, c;
+
+ for (i = 0; i < MAX_ELEM_ID; i++) {
+ ChannelElement *cce = ac->che[TYPE_CCE][i];
+ int index = 0;
+
+ if (cce && cce->coup.coupling_point == coupling_point) {
+ ChannelCoupling *coup = &cce->coup;
+
+ for (c = 0; c <= coup->num_coupled; c++) {
+ if (coup->type[c] == type && coup->id_select[c] == elem_id) {
+ if (coup->ch_select[c] != 1) {
+ apply_coupling_method(ac, &cc->ch[0], cce, index);
+ if (coup->ch_select[c] != 0)
+ index++;
+ }
+ if (coup->ch_select[c] != 2)
+ apply_coupling_method(ac, &cc->ch[1], cce, index++);
+ } else
+ index += 1 + (coup->ch_select[c] == 3);
+ }
+ }
+ }
+}
+
+/**
+ * Convert spectral data to float samples, applying all supported tools as appropriate.
+ */
+static void spectral_to_sample(AACContext *ac)
+{
+ int i, type;
+ for (type = 3; type >= 0; type--) {
+ for (i = 0; i < MAX_ELEM_ID; i++) {
+ ChannelElement *che = ac->che[type][i];
+ if (che) {
+ if (type <= TYPE_CPE)
+ apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
+ if (che->ch[0].ics.predictor_present) {
+ if (che->ch[0].ics.ltp.present)
+ ac->apply_ltp(ac, &che->ch[0]);
+ if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
+ ac->apply_ltp(ac, &che->ch[1]);
+ }
+ }
+ if (che->ch[0].tns.present)
+ ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
+ if (che->ch[1].tns.present)
+ ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
+ if (type <= TYPE_CPE)
+ apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
+ if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
+ ac->imdct_and_windowing(ac, &che->ch[0]);
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
+ ac->update_ltp(ac, &che->ch[0]);
+ if (type == TYPE_CPE) {
+ ac->imdct_and_windowing(ac, &che->ch[1]);
+ if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
+ ac->update_ltp(ac, &che->ch[1]);
+ }
+ if (ac->oc[1].m4ac.sbr > 0) {
+ AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
+ }
+ }
+ if (type <= TYPE_CCE)
+ apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
+
+#if CONFIG_AAC_FIXED
+ {
+ int j;
+ /* preparation for resampler */
+ for(j = 0; j<2048; j++){
+ che->ch[0].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[0].ret[j]<<7)+0x8000;
+ che->ch[1].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[1].ret[j]<<7)+0x8000;
+ }
+ }
+#endif /* CONFIG_AAC_FIXED */
+ }
+ }
+ }
+}
+
+static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
+{
+ int size;
+ AACADTSHeaderInfo hdr_info;
+ uint8_t layout_map[MAX_ELEM_ID*4][3];
+ int layout_map_tags, ret;
+
+ size = avpriv_aac_parse_header(gb, &hdr_info);
+ if (size > 0) {
+ if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
+ // This is 2 for "VLB " audio in NSV files.
+ // See samples/nsv/vlb_audio.
+ avpriv_report_missing_feature(ac->avctx,
+ "More than one AAC RDB per ADTS frame");
+ ac->warned_num_aac_frames = 1;
+ }
+ push_output_configuration(ac);
+ if (hdr_info.chan_config) {
+ ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
+ if ((ret = set_default_channel_config(ac->avctx,
+ layout_map,
+ &layout_map_tags,
+ hdr_info.chan_config)) < 0)
+ return ret;
+ if ((ret = output_configure(ac, layout_map, layout_map_tags,
+ FFMAX(ac->oc[1].status,
+ OC_TRIAL_FRAME), 0)) < 0)
+ return ret;
+ } else {
+ ac->oc[1].m4ac.chan_config = 0;
+ /**
+ * dual mono frames in Japanese DTV can have chan_config 0
+ * WITHOUT specifying PCE.
+ * thus, set dual mono as default.
+ */
+ if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
+ layout_map_tags = 2;
+ layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
+ layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
+ layout_map[0][1] = 0;
+ layout_map[1][1] = 1;
+ if (output_configure(ac, layout_map, layout_map_tags,
+ OC_TRIAL_FRAME, 0))
+ return -7;
+ }
+ }
+ ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
+ ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
+ ac->oc[1].m4ac.object_type = hdr_info.object_type;
+ if (ac->oc[0].status != OC_LOCKED ||
+ ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
+ ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
+ ac->oc[1].m4ac.sbr = -1;
+ ac->oc[1].m4ac.ps = -1;
+ }
+ if (!hdr_info.crc_absent)
+ skip_bits(gb, 16);
+ }
+ return size;
+}
+
+static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
+ int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
+{
+ AACContext *ac = avctx->priv_data;
+ ChannelElement *che = NULL, *che_prev = NULL;
+ enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
+ int err, elem_id;
+ int samples = 0, multiplier, audio_found = 0, pce_found = 0;
+ int is_dmono, sce_count = 0;
+
+ ac->frame = data;
+
+ if (show_bits(gb, 12) == 0xfff) {
+ if ((err = parse_adts_frame_header(ac, gb)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
+ goto fail;
+ }
+ if (ac->oc[1].m4ac.sampling_index > 12) {
+ av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
+ err = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ }
+
+ if ((err = frame_configure_elements(avctx)) < 0)
+ goto fail;
+
+ ac->tags_mapped = 0;
+ // parse
+ while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
+ elem_id = get_bits(gb, 4);
+
+ if (elem_type < TYPE_DSE) {
+ if (!(che=get_che(ac, elem_type, elem_id))) {
+ av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
+ elem_type, elem_id);
+ err = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ samples = 1024;
+ }
+
+ switch (elem_type) {
+
+ case TYPE_SCE:
+ err = decode_ics(ac, &che->ch[0], gb, 0, 0);
+ audio_found = 1;
+ sce_count++;
+ break;
+
+ case TYPE_CPE:
+ err = decode_cpe(ac, gb, che);
+ audio_found = 1;
+ break;
+
+ case TYPE_CCE:
+ err = decode_cce(ac, gb, che);
+ break;
+
+ case TYPE_LFE:
+ err = decode_ics(ac, &che->ch[0], gb, 0, 0);
+ audio_found = 1;
+ break;
+
+ case TYPE_DSE:
+ err = skip_data_stream_element(ac, gb);
+ break;
+
+ case TYPE_PCE: {
+ uint8_t layout_map[MAX_ELEM_ID*4][3];
+ int tags;
+ push_output_configuration(ac);
+ tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
+ if (tags < 0) {
+ err = tags;
+ break;
+ }
+ if (pce_found) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Not evaluating a further program_config_element as this construct is dubious at best.\n");
+ } else {
+ err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
+ if (!err)
+ ac->oc[1].m4ac.chan_config = 0;
+ pce_found = 1;
+ }
+ break;
+ }
+
+ case TYPE_FIL:
+ if (elem_id == 15)
+ elem_id += get_bits(gb, 8) - 1;
+ if (get_bits_left(gb) < 8 * elem_id) {
+ av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
+ err = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ while (elem_id > 0)
+ elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
+ err = 0; /* FIXME */
+ break;
+
+ default:
+ err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
+ break;
+ }
+
+ che_prev = che;
+ elem_type_prev = elem_type;
+
+ if (err)
+ goto fail;
+
+ if (get_bits_left(gb) < 3) {
+ av_log(avctx, AV_LOG_ERROR, overread_err);
+ err = AVERROR_INVALIDDATA;
+ goto fail;
+ }
+ }
+
+ spectral_to_sample(ac);
+
+ multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
+ samples <<= multiplier;
+ /* for dual-mono audio (SCE + SCE) */
+ is_dmono = ac->dmono_mode && sce_count == 2 &&
+ ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
+
+ if (samples)
+ ac->frame->nb_samples = samples;
+ else
+ av_frame_unref(ac->frame);
+ *got_frame_ptr = !!samples;
+
+ if (is_dmono) {
+ if (ac->dmono_mode == 1)
+ ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
+ else if (ac->dmono_mode == 2)
+ ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
+ }
+
+ if (ac->oc[1].status && audio_found) {
+ avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
+ avctx->frame_size = samples;
+ ac->oc[1].status = OC_LOCKED;
+ }
+
+ if (multiplier) {
+ int side_size;
+ const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
+ if (side && side_size>=4)
+ AV_WL32(side, 2*AV_RL32(side));
+ }
+ return 0;
+fail:
+ pop_output_configuration(ac);
+ return err;
+}
+
+static int aac_decode_frame(AVCodecContext *avctx, void *data,
+ int *got_frame_ptr, AVPacket *avpkt)
+{
+ AACContext *ac = avctx->priv_data;
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
+ GetBitContext gb;
+ int buf_consumed;
+ int buf_offset;
+ int err;
+ int new_extradata_size;
+ const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
+ AV_PKT_DATA_NEW_EXTRADATA,
+ &new_extradata_size);
+ int jp_dualmono_size;
+ const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
+ AV_PKT_DATA_JP_DUALMONO,
+ &jp_dualmono_size);
+
+ if (new_extradata && 0) {
+ av_free(avctx->extradata);
+ avctx->extradata = av_mallocz(new_extradata_size +
+ FF_INPUT_BUFFER_PADDING_SIZE);
+ if (!avctx->extradata)
+ return AVERROR(ENOMEM);
+ avctx->extradata_size = new_extradata_size;
+ memcpy(avctx->extradata, new_extradata, new_extradata_size);
+ push_output_configuration(ac);
+ if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
+ avctx->extradata,
+ avctx->extradata_size*8, 1) < 0) {
+ pop_output_configuration(ac);
+ return AVERROR_INVALIDDATA;
+ }
+ }
+
+ ac->dmono_mode = 0;
+ if (jp_dualmono && jp_dualmono_size > 0)
+ ac->dmono_mode = 1 + *jp_dualmono;
+ if (ac->force_dmono_mode >= 0)
+ ac->dmono_mode = ac->force_dmono_mode;
+
+ if (INT_MAX / 8 <= buf_size)
+ return AVERROR_INVALIDDATA;
+
+ if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0)
+ return err;
+
+ if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt)) < 0)
+ return err;
+
+ buf_consumed = (get_bits_count(&gb) + 7) >> 3;
+ for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
+ if (buf[buf_offset])
+ break;
+
+ return buf_size > buf_offset ? buf_consumed : buf_size;
+}
+
+static av_cold int aac_decode_close(AVCodecContext *avctx)
+{
+ AACContext *ac = avctx->priv_data;
+ int i, type;
+
+ for (i = 0; i < MAX_ELEM_ID; i++) {
+ for (type = 0; type < 4; type++) {
+ if (ac->che[type][i])
+ AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][i]->sbr);
+ av_freep(&ac->che[type][i]);
+ }
+ }
+
+ ff_mdct_end(&ac->mdct);
+ ff_mdct_end(&ac->mdct_small);
+ ff_mdct_end(&ac->mdct_ltp);
+ return 0;
+}
+
+static void aacdec_init(AACContext *c)
+{
+ c->imdct_and_windowing = imdct_and_windowing;
+ c->apply_ltp = apply_ltp;
+ c->apply_tns = apply_tns;
+ c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
+ c->update_ltp = update_ltp;
+#if CONFIG_AAC_FIXED
+ c->vector_pow43 = vector_pow43;
+ c->imdct_and_windowing_fixed = imdct_and_windowing;
+ c->subband_scale = subband_scale;
+#endif
+
+#if HAVE_MIPSFPU
+ ff_aacdec_init_mips(c);
+#endif
+}
+
+/**
+ * AVOptions for Japanese DTV specific extensions (ADTS only)
+ */
+#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
+static const AVOption options[] = {
+ {"dual_mono_mode", "Select the channel to decode for dual mono",
+ offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
+ AACDEC_FLAGS, "dual_mono_mode"},
+
+ {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
+ {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
+ {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
+ {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
+
+ {NULL},
+};
+
+static const AVClass aac_decoder_class = {
+ .class_name = "AAC decoder",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
diff --git a/libavcodec/aacdectab.h b/libavcodec/aacdectab.h
index 4a12b4f..16dd89f 100644
--- a/libavcodec/aacdectab.h
+++ b/libavcodec/aacdectab.h
@@ -38,9 +38,9 @@
/* @name ltp_coef
* Table of the LTP coefficients
*/
-static const float ltp_coef[8] = {
- 0.570829, 0.696616, 0.813004, 0.911304,
- 0.984900, 1.067894, 1.194601, 1.369533,
+static const INTFLOAT ltp_coef[8] = {
+ Q30(0.570829f), Q30(0.696616f), Q30(0.813004f), Q30(0.911304f),
+ Q30(0.984900f), Q30(1.067894f), Q30(1.194601f), Q30(1.369533f),
};
/* @name tns_tmp2_map
@@ -49,28 +49,28 @@ static const float ltp_coef[8] = {
* respectively.
* @{
*/
-static const float tns_tmp2_map_1_3[4] = {
- 0.00000000, -0.43388373, 0.64278758, 0.34202015,
+static const INTFLOAT tns_tmp2_map_1_3[4] = {
+ Q31(0.00000000f), Q31(-0.43388373f), Q31(0.64278758f), Q31(0.34202015f),
};
-static const float tns_tmp2_map_0_3[8] = {
- 0.00000000, -0.43388373, -0.78183150, -0.97492790,
- 0.98480773, 0.86602539, 0.64278758, 0.34202015,
+static const INTFLOAT tns_tmp2_map_0_3[8] = {
+ Q31(0.00000000f), Q31(-0.43388373f), Q31(-0.78183150f), Q31(-0.97492790f),
+ Q31(0.98480773f), Q31( 0.86602539f), Q31( 0.64278758f), Q31( 0.34202015f),
};
-static const float tns_tmp2_map_1_4[8] = {
- 0.00000000, -0.20791170, -0.40673664, -0.58778524,
- 0.67369562, 0.52643216, 0.36124167, 0.18374951,
+static const INTFLOAT tns_tmp2_map_1_4[8] = {
+ Q31(0.00000000f), Q31(-0.20791170f), Q31(-0.40673664f), Q31(-0.58778524f),
+ Q31(0.67369562f), Q31( 0.52643216f), Q31( 0.36124167f), Q31( 0.18374951f),
};
-static const float tns_tmp2_map_0_4[16] = {
- 0.00000000, -0.20791170, -0.40673664, -0.58778524,
- -0.74314481, -0.86602539, -0.95105654, -0.99452192,
- 0.99573416, 0.96182561, 0.89516330, 0.79801720,
- 0.67369562, 0.52643216, 0.36124167, 0.18374951,
+static const INTFLOAT tns_tmp2_map_0_4[16] = {
+ Q31( 0.00000000f), Q31(-0.20791170f), Q31(-0.40673664f), Q31(-0.58778524f),
+ Q31(-0.74314481f), Q31(-0.86602539f), Q31(-0.95105654f), Q31(-0.99452192f),
+ Q31( 0.99573416f), Q31( 0.96182561f), Q31( 0.89516330f), Q31( 0.79801720f),
+ Q31( 0.67369562f), Q31( 0.52643216f), Q31( 0.36124167f), Q31( 0.18374951f),
};
-static const float * const tns_tmp2_map[4] = {
+static const INTFLOAT * const tns_tmp2_map[4] = {
tns_tmp2_map_0_3,
tns_tmp2_map_0_4,
tns_tmp2_map_1_3,
diff --git a/libavcodec/aactab.c b/libavcodec/aactab.c
index 6cbb8c4..8953498 100644
--- a/libavcodec/aactab.c
+++ b/libavcodec/aactab.c
@@ -35,6 +35,8 @@
DECLARE_ALIGNED(32, float, ff_aac_kbd_long_1024)[1024];
DECLARE_ALIGNED(32, float, ff_aac_kbd_short_128)[128];
+DECLARE_ALIGNED(32, int, ff_aac_kbd_long_1024_fixed)[1024];
+DECLARE_ALIGNED(32, int, ff_aac_kbd_short_128_fixed)[128];
const uint8_t ff_aac_num_swb_1024[] = {
41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40, 40
diff --git a/libavcodec/aactab.h b/libavcodec/aactab.h
index 6ed3b4a..df32063 100644
--- a/libavcodec/aactab.h
+++ b/libavcodec/aactab.h
@@ -46,6 +46,8 @@
*/
DECLARE_ALIGNED(32, extern float, ff_aac_kbd_long_1024)[1024];
DECLARE_ALIGNED(32, extern float, ff_aac_kbd_short_128)[128];
+DECLARE_ALIGNED(32, extern int, ff_aac_kbd_long_1024_fixed)[1024];
+DECLARE_ALIGNED(32, extern int, ff_aac_kbd_short_128_fixed)[128];
// @}
/* @name number of scalefactor window bands for long and short transform windows respectively
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index c775b32..ec24cc9 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -310,6 +310,7 @@ void avcodec_register_all(void)
/* audio codecs */
REGISTER_ENCDEC (AAC, aac);
+ REGISTER_DECODER(AAC_FIXED, aac_fixed);
REGISTER_DECODER(AAC_LATM, aac_latm);
REGISTER_ENCDEC (AC3, ac3);
REGISTER_ENCODER(AC3_FIXED, ac3_fixed);
diff --git a/libavcodec/cbrt_fixed_tablegen.c b/libavcodec/cbrt_fixed_tablegen.c
new file mode 100644
index 0000000..996e237
--- /dev/null
+++ b/libavcodec/cbrt_fixed_tablegen.c
@@ -0,0 +1,24 @@
+/*
+ * Generate a header file for hardcoded AAC cube-root table
+ *
+ * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#define CONFIG_FIXED 1
+#include "cbrt_tablegen_template.c"
diff --git a/libavcodec/cbrt_tablegen.c b/libavcodec/cbrt_tablegen.c
index e0a8e63..8b14f46 100644
--- a/libavcodec/cbrt_tablegen.c
+++ b/libavcodec/cbrt_tablegen.c
@@ -20,18 +20,5 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <stdlib.h>
-#define CONFIG_HARDCODED_TABLES 0
-#include "cbrt_tablegen.h"
-#include "tableprint.h"
-
-int main(void)
-{
- cbrt_tableinit();
-
- write_fileheader();
-
- WRITE_ARRAY("static const", uint32_t, cbrt_tab);
-
- return 0;
-}
+#define CONFIG_FIXED 0
+#include "cbrt_tablegen_template.c"
diff --git a/libavcodec/cbrt_tablegen.h b/libavcodec/cbrt_tablegen.h
index a9d34dc..1afe458 100644
--- a/libavcodec/cbrt_tablegen.h
+++ b/libavcodec/cbrt_tablegen.h
@@ -26,13 +26,26 @@
#include <stdint.h>
#include <math.h>
+#if CONFIG_FIXED
+#define CBRT_RENAME(a) a ## _fixed
+#define CBRT(x) (int)floor((x).f * 8192 + 0.5)
+#else
+#define CBRT_RENAME(a) a
+#define CBRT(x) x.i
+#endif
+
#if CONFIG_HARDCODED_TABLES
+#if CONFIG_FIXED
+#define cbrt_tableinit_fixed()
+#include "libavcodec/cbrt_fixed_tables.h"
+#else
#define cbrt_tableinit()
#include "libavcodec/cbrt_tables.h"
+#endif
#else
static uint32_t cbrt_tab[1 << 13];
-static void cbrt_tableinit(void)
+static void CBRT_RENAME(cbrt_tableinit)(void)
{
if (!cbrt_tab[(1<<13) - 1]) {
int i;
@@ -42,7 +55,7 @@ static void cbrt_tableinit(void)
uint32_t i;
} f;
f.f = cbrtf(i) * i;
- cbrt_tab[i] = f.i;
+ cbrt_tab[i] = CBRT(f);
}
}
}
diff --git a/libavcodec/cbrt_tablegen_template.c b/libavcodec/cbrt_tablegen_template.c
new file mode 100644
index 0000000..0061141
--- /dev/null
+++ b/libavcodec/cbrt_tablegen_template.c
@@ -0,0 +1,37 @@
+/*
+ * Generate a header file for hardcoded AAC cube-root table
+ *
+ * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#define CONFIG_HARDCODED_TABLES 0
+#include "cbrt_tablegen.h"
+#include "tableprint.h"
+
+int main(void)
+{
+ CBRT_RENAME(cbrt_tableinit)();
+
+ write_fileheader();
+
+ WRITE_ARRAY("static const", uint32_t, cbrt_tab);
+
+ return 0;
+}
diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c
index 17de1d4..bbc495a 100644
--- a/libavcodec/dsputil.c
+++ b/libavcodec/dsputil.c
@@ -2507,6 +2507,17 @@ WRAPPER8_16_SQ(quant_psnr8x8_c, quant_psnr16_c)
WRAPPER8_16_SQ(rd8x8_c, rd16_c)
WRAPPER8_16_SQ(bit8x8_c, bit16_c)
+int ff_scalarproduct_q31_c(const int *v1, const int *v2, int len)
+{
+ long long p = 0;
+ int i;
+
+ for (i = 0; i < len; i++)
+ p += (long long)v1[i] * v2[i];
+
+ return (int)((p + 0x40000000) >> 31);
+}
+
static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini,
uint32_t maxi, uint32_t maxisign)
{
@@ -2885,6 +2896,7 @@ av_cold void ff_dsputil_init(DSPContext* c, AVCodecContext *avctx)
c->scalarproduct_and_madd_int16 = scalarproduct_and_madd_int16_c;
c->apply_window_int16 = apply_window_int16_c;
c->vector_clip_int32 = vector_clip_int32_c;
+ c->scalarproduct_q31 = ff_scalarproduct_q31_c;
c->shrink[0]= av_image_copy_plane;
c->shrink[1]= ff_shrink22;
diff --git a/libavcodec/dsputil.h b/libavcodec/dsputil.h
index b9f8bde..d11b047 100644
--- a/libavcodec/dsputil.h
+++ b/libavcodec/dsputil.h
@@ -208,6 +208,13 @@ typedef struct DSPContext {
void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
+ /**
+ * Calculate the scalar product of two vectors of floats.
+ * @param v1 first vector, 16-byte aligned
+ * @param v2 second vector, 16-byte aligned
+ * @param len length of vectors, multiple of 4
+ */
+ int (*scalarproduct_q31)(const int *v1, const int *v2, int len);
/* assume len is a multiple of 8, and arrays are 16-byte aligned */
void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
@@ -317,6 +324,16 @@ attribute_deprecated void dsputil_init(DSPContext* c, AVCodecContext *avctx);
int ff_check_alignment(void);
+/**
+ * Return the scalar product of two vectors.
+ *
+ * @param v1 first input vector
+ * @param v2 first input vector
+ * @param len number of elements
+ *
+ * @return sum of elementwise products
+ */
+int ff_scalarproduct_q31_c(const int *v1, const int *v2, int len);
void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type);
void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx);
diff --git a/libavcodec/float_emu.h b/libavcodec/float_emu.h
new file mode 100644
index 0000000..cee6b21
--- /dev/null
+++ b/libavcodec/float_emu.h
@@ -0,0 +1,400 @@
+/*
+ * Copyright (c) 2012
+ * MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of is
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author: Stanislav Ocovaj (stanislav.ocovaj imgtec com)
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_FLOAT_EMU_H
+#define AVUTIL_FLOAT_EMU_H
+
+#include "libavutil/common.h"
+#include "libavutil/intmath.h"
+
+extern const int divTable[128];
+extern const int sqrtTab[513];
+extern const int sqrExpMultTab[2];
+extern const int aac_costbl_1[16];
+extern const int aac_costbl_2[32];
+extern const int aac_sintbl_2[32];
+extern const int aac_costbl_3[32];
+extern const int aac_sintbl_3[32];
+extern const int aac_costbl_4[33];
+extern const int aac_sintbl_4[33];
+
+typedef struct aac_float_t {
+ int mant;
+ int expo;
+} aac_float_t;
+
+static const aac_float_t FLOAT_0 = { 0, 0};
+static const aac_float_t FLOAT_05 = { 536870912, 0};
+static const aac_float_t FLOAT_1 = { 536870912, 1};
+static const aac_float_t FLOAT_EPSILON = { 703687442, -16};
+static const aac_float_t FLOAT_1584893192 = { 850883053, 1};
+static const aac_float_t FLOAT_100000 = { 819200000, 17};
+static const aac_float_t FLOAT_0999999 = {1073740750, 0};
+
+static __inline aac_float_t int2float(const int x, const int exp)
+{
+ aac_float_t ret;
+ int nz;
+
+ if (x == 0)
+ {
+ ret.mant = 0;
+ ret.expo = 0;
+ }
+ else
+ {
+ ret.expo = exp;
+ ret.mant = x;
+ nz = 29 - ff_log2(FFABS(ret.mant));
+ ret.mant <<= nz;
+ ret.expo -= nz;
+ }
+
+ return ret;
+}
+
+static __inline aac_float_t float_add(aac_float_t a, aac_float_t b)
+{
+ int diff, nz;
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+ aac_float_t res;
+
+ if (manta == 0)
+ return b;
+
+ if (mantb == 0)
+ return a;
+
+ diff = expa - expb;
+ if (diff < 0) // expa < expb
+ {
+ diff = -diff;
+ if (diff >= 31)
+ manta = 0;
+ else if (diff != 0)
+ manta >>= diff;
+ expa = expb;
+ }
+ else // expa >= expb
+ {
+ if (diff >= 31)
+ mantb = 0;
+ else if (diff != 0)
+ mantb >>= diff;
+ }
+
+ manta = manta + mantb;
+ if (manta == 0)
+ expa = 0;
+ else
+ {
+ nz = 30 - ff_log2(FFABS(manta));
+ manta <<= nz;
+ manta >>= 1;
+ expa -= (nz-1);
+ }
+
+ res.mant = manta;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_sub(aac_float_t a, aac_float_t b)
+{
+ int diff, nz;
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+ aac_float_t res;
+
+ if (manta == 0)
+ {
+ res.mant = -mantb;
+ res.expo = expb;
+ return res;
+ }
+
+ if (mantb == 0)
+ return a;
+
+ diff = expa - expb;
+ if (diff < 0) // expa < expb
+ {
+ diff = -diff;
+ if (diff >= 31)
+ manta = 0;
+ else if (diff != 0)
+ manta >>= diff;
+ expa = expb;
+ }
+ else // expa >= expb
+ {
+ if (diff >= 31)
+ mantb = 0;
+ else if (diff != 0)
+ mantb >>= diff;
+ }
+
+ manta = manta - mantb;
+ if (manta == 0)
+ expa = 0;
+ else
+ {
+ nz = 30 - ff_log2(FFABS(manta));
+ manta <<= nz;
+ manta >>= 1;
+ expa -= (nz-1);
+ }
+
+ res.mant = manta;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_mul(aac_float_t a, aac_float_t b)
+{
+ aac_float_t res;
+ int mant;
+ int expa = a.expo;
+ int expb = b.expo;
+ long long accu;
+
+ expa = expa + expb;
+ accu = (long long)a.mant * b.mant;
+ mant = (int)((accu + 0x20000000) >> 30);
+ if (mant == 0)
+ expa = 0;
+ else if (mant < 536870912 && mant > -536870912)
+ {
+ mant <<= 1;
+ expa = expa - 1;
+ }
+ res.mant = mant;
+ res.expo = expa;
+
+ return res;
+}
+
+static __inline aac_float_t float_recip(const aac_float_t a)
+{
+ aac_float_t r;
+ int s;
+ int manta, expa;
+
+ manta = a.mant;
+ expa = a.expo;
+
+ expa = 1 - expa;
+ r.expo = expa;
+
+ s = manta >> 31;
+ manta = (manta ^ s) - s;
+
+ manta = divTable[(manta - 0x20000000) >> 22];
+
+ r.mant = (manta ^ s) - s;
+
+ return r;
+}
+
+static __inline aac_float_t float_div(aac_float_t a, aac_float_t b)
+{
+ aac_float_t res;
+ aac_float_t iB, tmp;
+ int mantb;
+
+ mantb = b.mant;
+ if (mantb != 0)
+ {
+ iB = float_recip(b);
+ // newton iteration to double precision
+ tmp = float_sub(FLOAT_1, float_mul(b, iB));
+ iB = float_add(iB, float_mul(iB, tmp));
+ res = float_mul(a, iB);
+ }
+ else
+ {
+ res.mant = 1;
+ res.expo = 2147483647;
+ }
+
+ return res;
+}
+
+static __inline int float_gt(aac_float_t a, aac_float_t b)
+{
+ int expa = a.expo;
+ int expb = b.expo;
+ int manta = a.mant;
+ int mantb = b.mant;
+
+ if (manta == 0)
+ expa = 0x80000000;
+
+ if (mantb == 0)
+ expb = 0x80000000;
+
+ if (expa > expb)
+ return 1;
+ else if (expa < expb)
+ return 0;
+ else // expa == expb
+ {
+ if (manta > mantb)
+ return 1;
+ else
+ return 0;
+ }
+}
+
+static __inline aac_float_t float_sqrt(aac_float_t val)
+{
+ int exp;
+ int tabIndex, rem;
+ int mant;
+ long long accu;
+ aac_float_t res;
+
+ exp = val.expo;
+ mant = val.mant;
+
+ if (mant == 0)
+ {
+ res.mant = 0;
+ res.expo = 0;
+ }
+ else
+ {
+ tabIndex = (mant - 536870912);
+ tabIndex = tabIndex >> 20;
+
+ rem = mant & 0xfffff;
+ accu = (long long)sqrtTab[tabIndex] * (0x100000-rem);
+ accu += (long long)sqrtTab[tabIndex+1] * rem;
+ mant = (int)((accu + 0x80000) >> 20);
+
+ accu = (long long)sqrExpMultTab[exp&1] * mant;
+ mant = (int)((accu + 0x10000000) >> 29);
+ if (mant < 1073741824)
+ exp -= 2;
+ else
+ mant >>= 1;
+
+ res.mant = mant;
+ res.expo = (exp>>1)+1;
+ }
+
+ return res;
+}
+
+static __inline void aac_fixed_sincos(int a, int *s, int *c)
+{
+ int idx, sign;
+ int sv, cv;
+ int st, ct;
+ long long accu;
+
+ idx = a >> 26;
+ sign = (idx << 27) >> 31;
+ cv = aac_costbl_1[idx & 0xf];
+ cv = (cv ^ sign) - sign;
+
+ idx -= 8;
+ sign = (idx << 27) >> 31;
+ sv = aac_costbl_1[idx & 0xf];
+ sv = (sv ^ sign) - sign;
+
+ idx = a >> 21;
+ ct = aac_costbl_2[idx & 0x1f];
+ st = aac_sintbl_2[idx & 0x1f];
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ idx = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ sv = (int)((accu + 0x20000000) >> 30);
+ cv = idx;
+
+ idx = a >> 16;
+ ct = aac_costbl_3[idx & 0x1f];
+ st = aac_sintbl_3[idx & 0x1f];
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ idx = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ sv = (int)((accu + 0x20000000) >> 30);
+ cv = idx;
+
+ idx = a >> 11;
+ accu = (long long)aac_costbl_4[idx & 0x1f]*(0x800 - (a&0x7ff));
+ accu += (long long)aac_costbl_4[(idx & 0x1f)+1]*(a&0x7ff);
+ ct = (int)((accu + 0x400) >> 11);
+ accu = (long long)aac_sintbl_4[idx & 0x1f]*(0x800 - (a&0x7ff));
+ accu += (long long)aac_sintbl_4[(idx & 0x1f)+1]*(a&0x7ff);
+ st = (int)((accu + 0x400) >> 11);
+
+ accu = (long long)cv*ct;
+ accu -= (long long)sv*st;
+ *c = (int)((accu + 0x20000000) >> 30);
+
+ accu = (long long)cv*st;
+ accu += (long long)sv*ct;
+ *s = (int)((accu + 0x20000000) >> 30);
+}
+
+#endif /* AVUTIL_FLOAT_EMU_H */
diff --git a/libavcodec/float_emu_tab.c b/libavcodec/float_emu_tab.c
new file mode 100644
index 0000000..fb0f829
--- /dev/null
+++ b/libavcodec/float_emu_tab.c
@@ -0,0 +1,296 @@
+/*
+ * Copyright (c) 2012
+ * MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of is
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author: Stanislav Ocovaj (stanislav.ocovaj imgtec com)
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+
+#define Q30(x) (int)((x)*1073741824.0 + 0.5)
+
+const int divTable[128] = {
+ Q30(0.9999999995), Q30(0.9922480620), Q30(0.9846153846), Q30(0.9770992366),
+ Q30(0.9696969697), Q30(0.9624060150), Q30(0.9552238807), Q30(0.9481481481),
+ Q30(0.9411764704), Q30(0.9343065694), Q30(0.9275362319), Q30(0.9208633094),
+ Q30(0.9142857143), Q30(0.9078014186), Q30(0.9014084507), Q30(0.8951048949),
+ Q30(0.8888888890), Q30(0.8827586207), Q30(0.8767123288), Q30(0.8707482992),
+ Q30(0.8648648649), Q30(0.8590604025), Q30(0.8533333335), Q30(0.8476821193),
+ Q30(0.8421052634), Q30(0.8366013071), Q30(0.8311688313), Q30(0.8258064515),
+ Q30(0.8205128205), Q30(0.8152866242), Q30(0.8101265822), Q30(0.8050314467),
+ Q30(0.7999999998), Q30(0.7950310558), Q30(0.7901234566), Q30(0.7852760735),
+ Q30(0.7804878047), Q30(0.7757575759), Q30(0.7710843375), Q30(0.7664670660),
+ Q30(0.7619047621), Q30(0.7573964498), Q30(0.7529411763), Q30(0.7485380117),
+ Q30(0.7441860465), Q30(0.7398843933), Q30(0.7356321840), Q30(0.7314285715),
+ Q30(0.7272727271), Q30(0.7231638418), Q30(0.7191011235), Q30(0.7150837989),
+ Q30(0.7111111111), Q30(0.7071823203), Q30(0.7032967033), Q30(0.6994535518),
+ Q30(0.6956521738), Q30(0.6918918919), Q30(0.6881720428), Q30(0.6844919785),
+ Q30(0.6808510637), Q30(0.6772486772), Q30(0.6736842105), Q30(0.6701570679),
+ Q30(0.6666666665), Q30(0.6632124353), Q30(0.6597938146), Q30(0.6564102564),
+ Q30(0.6530612246), Q30(0.6497461931), Q30(0.6464646463), Q30(0.6432160805),
+ Q30(0.6400000001), Q30(0.6368159205), Q30(0.6336633665), Q30(0.6305418718),
+ Q30(0.6274509802), Q30(0.6243902440), Q30(0.6213592235), Q30(0.6183574880),
+ Q30(0.6153846155), Q30(0.6124401912), Q30(0.6095238095), Q30(0.6066350709),
+ Q30(0.6037735851), Q30(0.6009389670), Q30(0.5981308413), Q30(0.5953488373),
+ Q30(0.5925925928), Q30(0.5898617511), Q30(0.5871559633), Q30(0.5844748858),
+ Q30(0.5818181820), Q30(0.5791855203), Q30(0.5765765766), Q30(0.5739910314),
+ Q30(0.5714285714), Q30(0.5688888887), Q30(0.5663716816), Q30(0.5638766522),
+ Q30(0.5614035088), Q30(0.5589519651), Q30(0.5565217393), Q30(0.5541125541),
+ Q30(0.5517241377), Q30(0.5493562231), Q30(0.5470085470), Q30(0.5446808510),
+ Q30(0.5423728814), Q30(0.5400843881), Q30(0.5378151261), Q30(0.5355648533),
+ Q30(0.5333333332), Q30(0.5311203320), Q30(0.5289256200), Q30(0.5267489711),
+ Q30(0.5245901640), Q30(0.5224489798), Q30(0.5203252034), Q30(0.5182186235),
+ Q30(0.5161290322), Q30(0.5140562248), Q30(0.5120000001), Q30(0.5099601592),
+ Q30(0.5079365079), Q30(0.5059288535), Q30(0.5039370079), Q30(0.5019607842)
+};
+
+const int sqrtTab[512+1] = { /* sqrt(x), 0.5<=x<1 */
+ Q30(0.7071067812), Q30(0.7077969783), Q30(0.7084865030), Q30(0.7091753576),
+ Q30(0.7098635430), Q30(0.7105510626), Q30(0.7112379172), Q30(0.7119241091),
+ Q30(0.7126096408), Q30(0.7132945131), Q30(0.7139787287), Q30(0.7146622892),
+ Q30(0.7153451964), Q30(0.7160274521), Q30(0.7167090587), Q30(0.7173900176),
+ Q30(0.7180703310), Q30(0.7187500000), Q30(0.7194290273), Q30(0.7201074138),
+ Q30(0.7207851619), Q30(0.7214622740), Q30(0.7221387504), Q30(0.7228145939),
+ Q30(0.7234898065), Q30(0.7241643891), Q30(0.7248383439), Q30(0.7255116729),
+ Q30(0.7261843774), Q30(0.7268564594), Q30(0.7275279206), Q30(0.7281987625),
+ Q30(0.7288689869), Q30(0.7295385958), Q30(0.7302075904), Q30(0.7308759727),
+ Q30(0.7315437444), Q30(0.7322109072), Q30(0.7328774626), Q30(0.7335434123),
+ Q30(0.7342087580), Q30(0.7348735011), Q30(0.7355376435), Q30(0.7362011867),
+ Q30(0.7368641328), Q30(0.7375264824), Q30(0.7381882383), Q30(0.7388494010),
+ Q30(0.7395099727), Q30(0.7401699550), Q30(0.7408293495), Q30(0.7414881573),
+ Q30(0.7421463802), Q30(0.7428040202), Q30(0.7434610785), Q30(0.7441175561),
+ Q30(0.7447734554), Q30(0.7454287778), Q30(0.7460835241), Q30(0.7467376967),
+ Q30(0.7473912966), Q30(0.7480443250), Q30(0.7486967845), Q30(0.7493486754),
+ Q30(0.7500000000), Q30(0.7506507593), Q30(0.7513009552), Q30(0.7519505885),
+ Q30(0.7525996612), Q30(0.7532481747), Q30(0.7538961302), Q30(0.7545435294),
+ Q30(0.7551903734), Q30(0.7558366638), Q30(0.7564824023), Q30(0.7571275900),
+ Q30(0.7577722282), Q30(0.7584163188), Q30(0.7590598627), Q30(0.7597028613),
+ Q30(0.7603453165), Q30(0.7609872287), Q30(0.7616286003), Q30(0.7622694322),
+ Q30(0.7629097258), Q30(0.7635494824), Q30(0.7641887036), Q30(0.7648273907),
+ Q30(0.7654655445), Q30(0.7661031671), Q30(0.7667402592), Q30(0.7673768224),
+ Q30(0.7680128580), Q30(0.7686483674), Q30(0.7692833515), Q30(0.7699178122),
+ Q30(0.7705517504), Q30(0.7711851676), Q30(0.7718180646), Q30(0.7724504434),
+ Q30(0.7730823047), Q30(0.7737136502), Q30(0.7743444811), Q30(0.7749747979),
+ Q30(0.7756046029), Q30(0.7762338966), Q30(0.7768626809), Q30(0.7774909567),
+ Q30(0.7781187249), Q30(0.7787459870), Q30(0.7793727447), Q30(0.7799989986),
+ Q30(0.7806247496), Q30(0.7812500000), Q30(0.7818747503), Q30(0.7824990018),
+ Q30(0.7831227556), Q30(0.7837460130), Q30(0.7843687749), Q30(0.7849910432),
+ Q30(0.7856128183), Q30(0.7862341017), Q30(0.7868548944), Q30(0.7874751980),
+ Q30(0.7880950132), Q30(0.7887143414), Q30(0.7893331838), Q30(0.7899515415),
+ Q30(0.7905694149), Q30(0.7911868063), Q30(0.7918037162), Q30(0.7924201456),
+ Q30(0.7930360963), Q30(0.7936515687), Q30(0.7942665643), Q30(0.7948810840),
+ Q30(0.7954951287), Q30(0.7961087003), Q30(0.7967217988), Q30(0.7973344265),
+ Q30(0.7979465835), Q30(0.7985582710), Q30(0.7991694906), Q30(0.7997802431),
+ Q30(0.8003905294), Q30(0.8010003511), Q30(0.8016097085), Q30(0.8022186034),
+ Q30(0.8028270360), Q30(0.8034350080), Q30(0.8040425205), Q30(0.8046495742),
+ Q30(0.8052561702), Q30(0.8058623099), Q30(0.8064679937), Q30(0.8070732229),
+ Q30(0.8076779991), Q30(0.8082823223), Q30(0.8088861941), Q30(0.8094896153),
+ Q30(0.8100925875), Q30(0.8106951108), Q30(0.8112971867), Q30(0.8118988159),
+ Q30(0.8125000000), Q30(0.8131007394), Q30(0.8137010355), Q30(0.8143008887),
+ Q30(0.8149003005), Q30(0.8154992717), Q30(0.8160978034), Q30(0.8166958964),
+ Q30(0.8172935518), Q30(0.8178907707), Q30(0.8184875534), Q30(0.8190839016),
+ Q30(0.8196798153), Q30(0.8202752969), Q30(0.8208703459), Q30(0.8214649642),
+ Q30(0.8220591522), Q30(0.8226529113), Q30(0.8232462420), Q30(0.8238391452),
+ Q30(0.8244316224), Q30(0.8250236739), Q30(0.8256153008), Q30(0.8262065044),
+ Q30(0.8267972847), Q30(0.8273876435), Q30(0.8279775814), Q30(0.8285670988),
+ Q30(0.8291561976), Q30(0.8297448778), Q30(0.8303331411), Q30(0.8309209873),
+ Q30(0.8315084185), Q30(0.8320954349), Q30(0.8326820373), Q30(0.8332682266),
+ Q30(0.8338540038), Q30(0.8344393703), Q30(0.8350243261), Q30(0.8356088721),
+ Q30(0.8361930102), Q30(0.8367767399), Q30(0.8373600631), Q30(0.8379429798),
+ Q30(0.8385254918), Q30(0.8391075991), Q30(0.8396893027), Q30(0.8402706035),
+ Q30(0.8408515030), Q30(0.8414320010), Q30(0.8420120990), Q30(0.8425917975),
+ Q30(0.8431710978), Q30(0.8437500000), Q30(0.8443285055), Q30(0.8449066146),
+ Q30(0.8454843285), Q30(0.8460616483), Q30(0.8466385738), Q30(0.8472151072),
+ Q30(0.8477912480), Q30(0.8483669977), Q30(0.8489423566), Q30(0.8495173263),
+ Q30(0.8500919067), Q30(0.8506660992), Q30(0.8512399043), Q30(0.8518133233),
+ Q30(0.8523863559), Q30(0.8529590038), Q30(0.8535312680), Q30(0.8541031480),
+ Q30(0.8546746457), Q30(0.8552457616), Q30(0.8558164961), Q30(0.8563868506),
+ Q30(0.8569568251), Q30(0.8575264211), Q30(0.8580956385), Q30(0.8586644791),
+ Q30(0.8592329426), Q30(0.8598010307), Q30(0.8603687435), Q30(0.8609360820),
+ Q30(0.8615030469), Q30(0.8620696389), Q30(0.8626358588), Q30(0.8632017071),
+ Q30(0.8637671852), Q30(0.8643322927), Q30(0.8648970313), Q30(0.8654614016),
+ Q30(0.8660254036), Q30(0.8665890391), Q30(0.8671523076), Q30(0.8677152111),
+ Q30(0.8682777495), Q30(0.8688399233), Q30(0.8694017339), Q30(0.8699631817),
+ Q30(0.8705242672), Q30(0.8710849914), Q30(0.8716453551), Q30(0.8722053585),
+ Q30(0.8727650028), Q30(0.8733242881), Q30(0.8738832157), Q30(0.8744417862),
+ Q30(0.8750000000), Q30(0.8755578580), Q30(0.8761153608), Q30(0.8766725087),
+ Q30(0.8772293031), Q30(0.8777857441), Q30(0.8783418327), Q30(0.8788975696),
+ Q30(0.8794529550), Q30(0.8800079902), Q30(0.8805626752), Q30(0.8811170114),
+ Q30(0.8816709989), Q30(0.8822246385), Q30(0.8827779307), Q30(0.8833308765),
+ Q30(0.8838834763), Q30(0.8844357310), Q30(0.8849876411), Q30(0.8855392071),
+ Q30(0.8860904300), Q30(0.8866413101), Q30(0.8871918479), Q30(0.8877420444),
+ Q30(0.8882919000), Q30(0.8888414158), Q30(0.8893905920), Q30(0.8899394292),
+ Q30(0.8904879279), Q30(0.8910360895), Q30(0.8915839135), Q30(0.8921314017),
+ Q30(0.8926785537), Q30(0.8932253704), Q30(0.8937718528), Q30(0.8943180013),
+ Q30(0.8948638164), Q30(0.8954092991), Q30(0.8959544492), Q30(0.8964992678),
+ Q30(0.8970437557), Q30(0.8975879136), Q30(0.8981317417), Q30(0.8986752401),
+ Q30(0.8992184107), Q30(0.8997612530), Q30(0.9003037680), Q30(0.9008459565),
+ Q30(0.9013878191), Q30(0.9019293557), Q30(0.9024705673), Q30(0.9030114547),
+ Q30(0.9035520186), Q30(0.9040922588), Q30(0.9046321767), Q30(0.9051717725),
+ Q30(0.9057110464), Q30(0.9062500000), Q30(0.9067886332), Q30(0.9073269465),
+ Q30(0.9078649404), Q30(0.9084026157), Q30(0.9089399735), Q30(0.9094770132),
+ Q30(0.9100137362), Q30(0.9105501426), Q30(0.9110862338), Q30(0.9116220092),
+ Q30(0.9121574699), Q30(0.9126926167), Q30(0.9132274496), Q30(0.9137619697),
+ Q30(0.9142961772), Q30(0.9148300732), Q30(0.9153636573), Q30(0.9158969307),
+ Q30(0.9164298936), Q30(0.9169625468), Q30(0.9174948912), Q30(0.9180269265),
+ Q30(0.9185586534), Q30(0.9190900731), Q30(0.9196211854), Q30(0.9201519913),
+ Q30(0.9206824913), Q30(0.9212126858), Q30(0.9217425752), Q30(0.9222721602),
+ Q30(0.9228014410), Q30(0.9233304188), Q30(0.9238590938), Q30(0.9243874662),
+ Q30(0.9249155368), Q30(0.9254433061), Q30(0.9259707746), Q30(0.9264979423),
+ Q30(0.9270248110), Q30(0.9275513799), Q30(0.9280776503), Q30(0.9286036226),
+ Q30(0.9291292969), Q30(0.9296546737), Q30(0.9301797543), Q30(0.9307045382),
+ Q30(0.9312290265), Q30(0.9317532196), Q30(0.9322771183), Q30(0.9328007223),
+ Q30(0.9333240325), Q30(0.9338470497), Q30(0.9343697741), Q30(0.9348922065),
+ Q30(0.9354143469), Q30(0.9359361958), Q30(0.9364577541), Q30(0.9369790219),
+ Q30(0.9375000000), Q30(0.9380206889), Q30(0.9385410887), Q30(0.9390612002),
+ Q30(0.9395810235), Q30(0.9401005600), Q30(0.9406198091), Q30(0.9411387718),
+ Q30(0.9416574482), Q30(0.9421758396), Q30(0.9426939455), Q30(0.9432117669),
+ Q30(0.9437293042), Q30(0.9442465580), Q30(0.9447635286), Q30(0.9452802162),
+ Q30(0.9457966220), Q30(0.9463127456), Q30(0.9468285879), Q30(0.9473441495),
+ Q30(0.9478594307), Q30(0.9483744316), Q30(0.9488891531), Q30(0.9494035956),
+ Q30(0.9499177597), Q30(0.9504316454), Q30(0.9509452535), Q30(0.9514585841),
+ Q30(0.9519716380), Q30(0.9524844158), Q30(0.9529969175), Q30(0.9535091440),
+ Q30(0.9540210953), Q30(0.9545327718), Q30(0.9550441746), Q30(0.9555553030),
+ Q30(0.9560661586), Q30(0.9565767418), Q30(0.9570870521), Q30(0.9575970904),
+ Q30(0.9581068573), Q30(0.9586163531), Q30(0.9591255784), Q30(0.9596345332),
+ Q30(0.9601432183), Q30(0.9606516343), Q30(0.9611597811), Q30(0.9616676597),
+ Q30(0.9621752701), Q30(0.9626826127), Q30(0.9631896880), Q30(0.9636964966),
+ Q30(0.9642030387), Q30(0.9647093150), Q30(0.9652153258), Q30(0.9657210712),
+ Q30(0.9662265521), Q30(0.9667317686), Q30(0.9672367214), Q30(0.9677414102),
+ Q30(0.9682458364), Q30(0.9687500000), Q30(0.9692539014), Q30(0.9697575406),
+ Q30(0.9702609186), Q30(0.9707640354), Q30(0.9712668918), Q30(0.9717694880),
+ Q30(0.9722718243), Q30(0.9727739012), Q30(0.9732757187), Q30(0.9737772783),
+ Q30(0.9742785795), Q30(0.9747796226), Q30(0.9752804083), Q30(0.9757809374),
+ Q30(0.9762812094), Q30(0.9767812253), Q30(0.9772809856), Q30(0.9777804906),
+ Q30(0.9782797401), Q30(0.9787787353), Q30(0.9792774762), Q30(0.9797759629),
+ Q30(0.9802741962), Q30(0.9807721768), Q30(0.9812699044), Q30(0.9817673797),
+ Q30(0.9822646030), Q30(0.9827615744), Q30(0.9832582953), Q30(0.9837547648),
+ Q30(0.9842509842), Q30(0.9847469535), Q30(0.9852426732), Q30(0.9857381433),
+ Q30(0.9862333648), Q30(0.9867283376), Q30(0.9872230627), Q30(0.9877175395),
+ Q30(0.9882117687), Q30(0.9887057510), Q30(0.9891994870), Q30(0.9896929762),
+ Q30(0.9901862200), Q30(0.9906792180), Q30(0.9911719705), Q30(0.9916644781),
+ Q30(0.9921567417), Q30(0.9926487608), Q30(0.9931405364), Q30(0.9936320684),
+ Q30(0.9941233573), Q30(0.9946144037), Q30(0.9951052079), Q30(0.9955957700),
+ Q30(0.9960860908), Q30(0.9965761700), Q30(0.9970660084), Q30(0.9975556061),
+ Q30(0.9980449639), Q30(0.9985340820), Q30(0.9990229602), Q30(0.9995115995),
+ 0x3FFFFFFF
+};
+
+const int sqrExpMultTab[2] = {
+ Q30(0.5000000000), Q30(0.7071067812)
+};
+
+const int aac_costbl_1[16] = {
+ Q30( 1.000000000000000), Q30( 0.980785280403230), Q30( 0.923879532511287), Q30( 0.831469612302545),
+ Q30( 0.707106781186548), Q30( 0.555570233019602), Q30( 0.382683432365090), Q30( 0.195090322016128),
+ Q30( 0.000000000000000), Q30(-0.195090322016128), Q30(-0.382683432365090), Q30(-0.555570233019602),
+ Q30(-0.707106781186547), Q30(-0.831469612302545), Q30(-0.923879532511287), Q30(-0.980785280403230)
+};
+
+const int aac_costbl_2[32] = {
+ Q30(1.000000000000000), Q30(0.999981175282601), Q30(0.999924701839145), Q30(0.999830581795823),
+ Q30(0.999698818696204), Q30(0.999529417501093), Q30(0.999322384588350), Q30(0.999077727752645),
+ Q30(0.998795456205172), Q30(0.998475580573295), Q30(0.998118112900149), Q30(0.997723066644192),
+ Q30(0.997290456678690), Q30(0.996820299291166), Q30(0.996312612182778), Q30(0.995767414467660),
+ Q30(0.995184726672197), Q30(0.994564570734255), Q30(0.993906970002356), Q30(0.993211949234795),
+ Q30(0.992479534598710), Q30(0.991709753669100), Q30(0.990902635427780), Q30(0.990058210262297),
+ Q30(0.989176509964781), Q30(0.988257567730749), Q30(0.987301418157858), Q30(0.986308097244599),
+ Q30(0.985277642388941), Q30(0.984210092386929), Q30(0.983105487431216), Q30(0.981963869109555)
+};
+
+const int aac_sintbl_2[32] = {
+ Q30(0.000000000000000), Q30(0.006135884649154), Q30(0.012271538285720), Q30(0.018406729905805),
+ Q30(0.024541228522912), Q30(0.030674803176637), Q30(0.036807222941359), Q30(0.042938256934941),
+ Q30(0.049067674327418), Q30(0.055195244349690), Q30(0.061320736302209), Q30(0.067443919563664),
+ Q30(0.073564563599667), Q30(0.079682437971430), Q30(0.085797312344440), Q30(0.091908956497133),
+ Q30(0.098017140329561), Q30(0.104121633872055), Q30(0.110222207293883), Q30(0.116318630911905),
+ Q30(0.122410675199216), Q30(0.128498110793793), Q30(0.134580708507126), Q30(0.140658239332849),
+ Q30(0.146730474455362), Q30(0.152797185258443), Q30(0.158858143333861), Q30(0.164913120489970),
+ Q30(0.170961888760301), Q30(0.177004220412149), Q30(0.183039887955141), Q30(0.189068664149806)
+};
+
+const int aac_costbl_3[32] = {
+ Q30(1.000000000000000), Q30(0.999999981616429), Q30(0.999999926465718), Q30(0.999999834547868),
+ Q30(0.999999705862882), Q30(0.999999540410766), Q30(0.999999338191526), Q30(0.999999099205168),
+ Q30(0.999998823451702), Q30(0.999998510931138), Q30(0.999998161643487), Q30(0.999997775588762),
+ Q30(0.999997352766978), Q30(0.999996893178150), Q30(0.999996396822294), Q30(0.999995863699430),
+ Q30(0.999995293809576), Q30(0.999994687152754), Q30(0.999994043728986), Q30(0.999993363538295),
+ Q30(0.999992646580707), Q30(0.999991892856248), Q30(0.999991102364946), Q30(0.999990275106829),
+ Q30(0.999989411081928), Q30(0.999988510290276), Q30(0.999987572731904), Q30(0.999986598406848),
+ Q30(0.999985587315143), Q30(0.999984539456827), Q30(0.999983454831938), Q30(0.999982333440515)
+};
+
+const int aac_sintbl_3[32] = {
+ Q30(0.000000000000000), Q30(0.000191747597311), Q30(0.000383495187571), Q30(0.000575242763732),
+ Q30(0.000766990318743), Q30(0.000958737845553), Q30(0.001150485337114), Q30(0.001342232786374),
+ Q30(0.001533980186285), Q30(0.001725727529795), Q30(0.001917474809855), Q30(0.002109222019416),
+ Q30(0.002300969151426), Q30(0.002492716198836), Q30(0.002684463154596), Q30(0.002876210011656),
+ Q30(0.003067956762966), Q30(0.003259703401476), Q30(0.003451449920136), Q30(0.003643196311896),
+ Q30(0.003834942569706), Q30(0.004026688686517), Q30(0.004218434655277), Q30(0.004410180468938),
+ Q30(0.004601926120449), Q30(0.004793671602760), Q30(0.004985416908822), Q30(0.005177162031584),
+ Q30(0.005368906963996), Q30(0.005560651699010), Q30(0.005752396229574), Q30(0.005944140548639)
+};
+
+const int aac_costbl_4[33] = {
+ Q30(1.000000000000000), Q30(0.999999999982047), Q30(0.999999999928189), Q30(0.999999999838426),
+ Q30(0.999999999712757), Q30(0.999999999551182), Q30(0.999999999353703), Q30(0.999999999120317),
+ Q30(0.999999998851027), Q30(0.999999998545831), Q30(0.999999998204729), Q30(0.999999997827723),
+ Q30(0.999999997414810), Q30(0.999999996965993), Q30(0.999999996481270), Q30(0.999999995960641),
+ Q30(0.999999995404107), Q30(0.999999994811668), Q30(0.999999994183323), Q30(0.999999993519073),
+ Q30(0.999999992818918), Q30(0.999999992082857), Q30(0.999999991310890), Q30(0.999999990503019),
+ Q30(0.999999989659241), Q30(0.999999988779559), Q30(0.999999987863971), Q30(0.999999986912477),
+ Q30(0.999999985925079), Q30(0.999999984901774), Q30(0.999999983842565), Q30(0.999999982747450),
+ Q30(0.999999981616429)
+};
+
+const int aac_sintbl_4[33] = {
+ Q30(0.000000000000000), Q30(0.000005992112453), Q30(0.000011984224905), Q30(0.000017976337357),
+ Q30(0.000023968449808), Q30(0.000029960562259), Q30(0.000035952674708), Q30(0.000041944787156),
+ Q30(0.000047936899603), Q30(0.000053929012048), Q30(0.000059921124491), Q30(0.000065913236932),
+ Q30(0.000071905349370), Q30(0.000077897461806), Q30(0.000083889574239), Q30(0.000089881686669),
+ Q30(0.000095873799096), Q30(0.000101865911519), Q30(0.000107858023939), Q30(0.000113850136355),
+ Q30(0.000119842248767), Q30(0.000125834361174), Q30(0.000131826473577), Q30(0.000137818585975),
+ Q30(0.000143810698369), Q30(0.000149802810757), Q30(0.000155794923139), Q30(0.000161787035517),
+ Q30(0.000167779147888), Q30(0.000173771260253), Q30(0.000179763372612), Q30(0.000185755484965),
+ Q30(0.000191747597311)
+};
diff --git a/libavcodec/fmtconvert.c b/libavcodec/fmtconvert.c
index fb4302c..8ec5db0 100644
--- a/libavcodec/fmtconvert.c
+++ b/libavcodec/fmtconvert.c
@@ -68,6 +68,22 @@ static void float_to_int16_interleave_c(int16_t *dst, const float **src,
}
}
+static void int_to_int16_interleave_c(int16_t *dst, const int **src, long len, int channels)
+{
+ int i, j, c;
+
+ if (channels==2){
+ for (i=0; i<len; i++){
+ dst[2*i] = av_clip_int16((*(src[0]+i) + 256) >> 9);
+ dst[2*i+1] = av_clip_int16((*(src[1]+i) + 256) >> 9);
+ }
+ }else{
+ for (c=0; c<channels; c++)
+ for (i=0, j=c; i<len; i++, j+=channels)
+ dst[j] = av_clip_int16((*(src[c]+i) + 256) >> 9);
+ }
+}
+
void ff_float_interleave_c(float *dst, const float **src, unsigned int len,
int channels)
{
@@ -93,12 +109,13 @@ av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
c->int32_to_float_fmul_array8 = int32_to_float_fmul_array8_c;
c->float_to_int16 = float_to_int16_c;
c->float_to_int16_interleave = float_to_int16_interleave_c;
+ c->int_to_int16_interleave = int_to_int16_interleave_c;
c->float_interleave = ff_float_interleave_c;
if (ARCH_ARM) ff_fmt_convert_init_arm(c, avctx);
if (ARCH_PPC) ff_fmt_convert_init_ppc(c, avctx);
if (ARCH_X86) ff_fmt_convert_init_x86(c, avctx);
- if (HAVE_MIPSFPU) ff_fmt_convert_init_mips(c);
+ if (ARCH_MIPS) ff_fmt_convert_init_mips(c);
}
/* ffdshow custom code */
diff --git a/libavcodec/fmtconvert.h b/libavcodec/fmtconvert.h
index 30abcc3..2aeb173 100644
--- a/libavcodec/fmtconvert.h
+++ b/libavcodec/fmtconvert.h
@@ -86,6 +86,8 @@ typedef struct FmtConvertContext {
void (*float_to_int16_interleave)(int16_t *dst, const float **src,
long len, int channels);
+ void (*int_to_int16_interleave)(int16_t *dst, const int **src,
+ long len, int channels);
/**
* Convert multiple arrays of float to an array of interleaved float.
*
diff --git a/libavcodec/lpc.h b/libavcodec/lpc.h
index c323230..e02ea77 100644
--- a/libavcodec/lpc.h
+++ b/libavcodec/lpc.h
@@ -194,4 +194,52 @@ static inline int compute_lpc_coefs(const LPC_TYPE *autoc, int max_order,
return 0;
}
+static inline int compute_lpc_coefs_fixed(const int *autoc, int max_order, int *lpc,
+ int lpc_stride, int fail, int normalize)
+{
+ int i, j;
+ int *lpc_last = lpc;
+ int err;
+ long long accu;
+
+ av_assert2(normalize || !fail);
+
+ if (normalize)
+ err = *autoc++;
+
+ if (fail && (autoc[max_order - 1] == 0 || err <= 0))
+ return -1;
+
+ for(i=0; i<max_order; i++) {
+ int r = (-autoc[i] + 16) >> 5;
+
+ if (normalize) {
+ for(j=0; j<i; j++)
+ r -= lpc_last[j] * autoc[i-j-1];
+
+ r /= err;
+ err *= 1 - (r * r);
+ }
+
+ lpc[i] = r;
+
+ for (j=0; j < (i+1)>>1; j++) {
+ int f = lpc_last[ j];
+ int b = lpc_last[i-1-j];
+
+ accu = (long long)r * b;
+ lpc[ j] = f + (int)((accu + 0x2000000) >> 26);
+ accu = (long long)r * f;
+ lpc[i-1-j] = b + (int)((accu + 0x2000000) >> 26);
+ }
+
+ if (fail && err < 0)
+ return -1;
+
+ lpc_last = lpc;
+ lpc += lpc_stride;
+ }
+ return 0;
+}
+
#endif /* AVCODEC_LPC_H */
diff --git a/libavcodec/mdct.c b/libavcodec/mdct.c
index 922d577..d1c00a1 100644
--- a/libavcodec/mdct.c
+++ b/libavcodec/mdct.c
@@ -81,8 +81,13 @@ av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale)
scale = sqrt(fabs(scale));
for(i=0;i<n4;i++) {
alpha = 2 * M_PI * (i + theta) / n;
+#if CONFIG_FFT_FIXED_32
+ s->tcos[i*tstep] = (FFTSample)floor(-cos(alpha) * 2147483648.0 + 0.5);
+ s->tsin[i*tstep] = (FFTSample)floor(-sin(alpha) * 2147483648.0 + 0.5);
+#else
s->tcos[i*tstep] = FIX15(-cos(alpha) * scale);
s->tsin[i*tstep] = FIX15(-sin(alpha) * scale);
+#endif
}
return 0;
fail:
diff --git a/libavcodec/mips/Makefile b/libavcodec/mips/Makefile
index 6537b43..233c3ad 100644
--- a/libavcodec/mips/Makefile
+++ b/libavcodec/mips/Makefile
@@ -10,11 +10,11 @@ MIPSFPU-OBJS-$(CONFIG_AMRWB_DECODER) += mips/acelp_filters_mips.o \
MIPSFPU-OBJS-$(CONFIG_MPEGAUDIODSP) += mips/mpegaudiodsp_mips_float.o
MIPSDSPR1-OBJS-$(CONFIG_MPEGAUDIODSP) += mips/mpegaudiodsp_mips_fixed.o
MIPSFPU-OBJS-$(CONFIG_FFT) += mips/fft_mips.o
-MIPSFPU-OBJS += mips/fmtconvert_mips.o
+OBJS += mips/fmtconvert_mips.o
OBJS-$(CONFIG_AC3DSP) += mips/ac3dsp_mips.o
-OBJS-$(CONFIG_AAC_DECODER) += mips/aacdec_mips.o \
- mips/aacsbr_mips.o \
- mips/sbrdsp_mips.o \
- mips/aacpsdsp_mips.o
+MIPSFPU-OBJS-$(CONFIG_AAC_DECODER) += mips/aacdec_mips.o
+OBJS-$(CONFIG_AAC_DECODER) += mips/aacsbr_mips.o \
+ mips/sbrdsp_mips.o
+OBJS += mips/aacpsdsp_mips.o
MIPSDSPR1-OBJS-$(CONFIG_AAC_ENCODER) += mips/aaccoder_mips.o
MIPSFPU-OBJS-$(CONFIG_AAC_ENCODER) += mips/iirfilter_mips.o
diff --git a/libavcodec/mips/aacdec_mips.c b/libavcodec/mips/aacdec_mips.c
index e403366..d42529f 100644
--- a/libavcodec/mips/aacdec_mips.c
+++ b/libavcodec/mips/aacdec_mips.c
@@ -484,7 +484,6 @@ static void apply_ltp_mips(AACContext *ac, SingleChannelElement *sce)
}
}
-#if HAVE_MIPSFPU
static void update_ltp_mips(AACContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
@@ -816,16 +815,13 @@ static void update_ltp_mips(AACContext *ac, SingleChannelElement *sce)
);
}
}
-#endif /* HAVE_MIPSFPU */
-#endif /* HAVE_INLINE_ASM */
+#endif /* HAVE_INLINE_ASM*/
void ff_aacdec_init_mips(AACContext *c)
{
#if HAVE_INLINE_ASM
c->imdct_and_windowing = imdct_and_windowing_mips;
c->apply_ltp = apply_ltp_mips;
-#if HAVE_MIPSFPU
c->update_ltp = update_ltp_mips;
-#endif /* HAVE_MIPSFPU */
#endif /* HAVE_INLINE_ASM */
}
diff --git a/libavcodec/mips/fmtconvert_mips.c b/libavcodec/mips/fmtconvert_mips.c
index 8a0265f..03e670c 100644
--- a/libavcodec/mips/fmtconvert_mips.c
+++ b/libavcodec/mips/fmtconvert_mips.c
@@ -52,6 +52,7 @@
#include "libavcodec/fmtconvert.h"
#if HAVE_INLINE_ASM
+#if HAVE_MIPSFPU
#if HAVE_MIPSDSPR1
static void float_to_int16_mips(int16_t *dst, const float *src, long len)
{
@@ -328,15 +329,18 @@ static void int32_to_float_fmul_scalar_mips(float *dst, const int *src,
: "memory"
);
}
+#endif /* HAVE_MIPSFPU */
#endif /* HAVE_INLINE_ASM */
av_cold void ff_fmt_convert_init_mips(FmtConvertContext *c)
{
#if HAVE_INLINE_ASM
+#if HAVE_MIPSFPU
#if HAVE_MIPSDSPR1
c->float_to_int16_interleave = float_to_int16_interleave_mips;
c->float_to_int16 = float_to_int16_mips;
#endif
c->int32_to_float_fmul_scalar = int32_to_float_fmul_scalar_mips;
#endif
+#endif
}
diff --git a/libavcodec/sinewin.c b/libavcodec/sinewin.c
index 1fa0e95..f6281f4 100644
--- a/libavcodec/sinewin.c
+++ b/libavcodec/sinewin.c
@@ -16,5 +16,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#define CONFIG_FIXED 0
#include "sinewin.h"
#include "sinewin_tablegen.h"
diff --git a/libavcodec/sinewin.h b/libavcodec/sinewin.h
index 2268fd5..d0cfc7c 100644
--- a/libavcodec/sinewin.h
+++ b/libavcodec/sinewin.h
@@ -30,20 +30,32 @@
# define SINETABLE_CONST
#endif
+#ifndef CONFIG_FIXED
+#define CONFIG_FIXED 0
+#endif
+
+#if CONFIG_FIXED
+#define SINEWIN_SUFFIX(a) a ## _fixed
+#define INTFLOAT int
+#else
+#define SINEWIN_SUFFIX(a) a
+#define INTFLOAT float
+#endif
+
#define SINETABLE(size) \
- SINETABLE_CONST DECLARE_ALIGNED(32, float, ff_sine_##size)[size]
+ SINETABLE_CONST DECLARE_ALIGNED(32, INTFLOAT, SINEWIN_SUFFIX(ff_sine_##size))[size]
/**
* Generate a sine window.
* @param window pointer to half window
* @param n size of half window
*/
-void ff_sine_window_init(float *window, int n);
+void SINEWIN_SUFFIX(ff_sine_window_init)(INTFLOAT *window, int n);
/**
* initialize the specified entry of ff_sine_windows
*/
-void ff_init_ff_sine_windows(int index);
+void SINEWIN_SUFFIX(ff_init_ff_sine_windows)(int index);
extern SINETABLE( 32);
extern SINETABLE( 64);
@@ -55,6 +67,6 @@ extern SINETABLE(2048);
extern SINETABLE(4096);
extern SINETABLE(8192);
-extern SINETABLE_CONST float * const ff_sine_windows[14];
+extern SINETABLE_CONST INTFLOAT * const SINEWIN_SUFFIX(ff_sine_windows)[14];
#endif /* AVCODEC_SINEWIN_H */
diff --git a/libavcodec/sinewin_fixed.c b/libavcodec/sinewin_fixed.c
new file mode 100644
index 0000000..753e6ed
--- /dev/null
+++ b/libavcodec/sinewin_fixed.c
@@ -0,0 +1,21 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#define CONFIG_FIXED 1
+#include "sinewin.h"
+#include "sinewin_tablegen.h"
diff --git a/libavcodec/sinewin_fixed_tablegen.c b/libavcodec/sinewin_fixed_tablegen.c
new file mode 100644
index 0000000..10deaa3
--- /dev/null
+++ b/libavcodec/sinewin_fixed_tablegen.c
@@ -0,0 +1,24 @@
+/*
+ * Generate a header file for hardcoded sine windows
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#define CONFIG_FIXED 1
+#include "sinewin_tablegen_template.c"
diff --git a/libavcodec/sinewin_tablegen.c b/libavcodec/sinewin_tablegen.c
index 561ae3e..75c599d 100644
--- a/libavcodec/sinewin_tablegen.c
+++ b/libavcodec/sinewin_tablegen.c
@@ -20,27 +20,5 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <stdlib.h>
-#define CONFIG_HARDCODED_TABLES 0
-#define SINETABLE_CONST
-#define SINETABLE(size) \
- float ff_sine_##size[size]
-#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
-#include "sinewin_tablegen.h"
-#include "tableprint.h"
-
-int main(void)
-{
- int i;
-
- write_fileheader();
-
- for (i = 5; i <= 13; i++) {
- ff_init_ff_sine_windows(i);
- printf("SINETABLE(%4i) = {\n", 1 << i);
- write_float_array(ff_sine_windows[i], 1 << i);
- printf("};\n");
- }
-
- return 0;
-}
+#define CONFIG_FIXED 0
+#include "sinewin_tablegen_template.c"
diff --git a/libavcodec/sinewin_tablegen.h b/libavcodec/sinewin_tablegen.h
index 2b9c4f2..6d6155d 100644
--- a/libavcodec/sinewin_tablegen.h
+++ b/libavcodec/sinewin_tablegen.h
@@ -41,26 +41,41 @@ SINETABLE(2048);
SINETABLE(4096);
SINETABLE(8192);
#else
+#if CONFIG_FIXED
+#include "libavcodec/sinewin_fixed_tables.h"
+#else
#include "libavcodec/sinewin_tables.h"
#endif
+#endif
+
+#if CONFIG_FIXED
+#define SINEWIN_SUFFIX(a) a ## _fixed
+#define INTFLOAT int
+#define SIN_FIX(a) (int)floor((a) * 0x80000000 + 0.5)
+#else
+#define SINEWIN_SUFFIX(a) a
+#define INTFLOAT float
+#define SIN_FIX(a) a
+#endif
-SINETABLE_CONST float * const ff_sine_windows[] = {
+SINETABLE_CONST INTFLOAT * const SINEWIN_SUFFIX(ff_sine_windows)[] = {
NULL, NULL, NULL, NULL, NULL, // unused
- ff_sine_32 , ff_sine_64 ,
- ff_sine_128, ff_sine_256, ff_sine_512, ff_sine_1024, ff_sine_2048, ff_sine_4096, ff_sine_8192
+ SINEWIN_SUFFIX(ff_sine_32) , SINEWIN_SUFFIX(ff_sine_64), SINEWIN_SUFFIX(ff_sine_128),
+ SINEWIN_SUFFIX(ff_sine_256), SINEWIN_SUFFIX(ff_sine_512), SINEWIN_SUFFIX(ff_sine_1024),
+ SINEWIN_SUFFIX(ff_sine_2048), SINEWIN_SUFFIX(ff_sine_4096), SINEWIN_SUFFIX(ff_sine_8192)
};
// Generate a sine window.
-av_cold void ff_sine_window_init(float *window, int n) {
+av_cold void SINEWIN_SUFFIX(ff_sine_window_init)(INTFLOAT *window, int n) {
int i;
for(i = 0; i < n; i++)
- window[i] = sinf((i + 0.5) * (M_PI / (2.0 * n)));
+ window[i] = SIN_FIX(sinf((i + 0.5) * (M_PI / (2.0 * n))));
}
-av_cold void ff_init_ff_sine_windows(int index) {
- assert(index >= 0 && index < FF_ARRAY_ELEMS(ff_sine_windows));
+av_cold void SINEWIN_SUFFIX(ff_init_ff_sine_windows)(int index) {
+ assert(index >= 0 && index < FF_ARRAY_ELEMS(SINEWIN_SUFFIX(ff_sine_windows)));
#if !CONFIG_HARDCODED_TABLES
- ff_sine_window_init(ff_sine_windows[index], 1 << index);
+ SINEWIN_SUFFIX(ff_sine_window_init)(SINEWIN_SUFFIX(ff_sine_windows)[index], 1 << index);
#endif
}
diff --git a/libavcodec/sinewin_tablegen_template.c b/libavcodec/sinewin_tablegen_template.c
new file mode 100644
index 0000000..498e792
--- /dev/null
+++ b/libavcodec/sinewin_tablegen_template.c
@@ -0,0 +1,60 @@
+/*
+ * Generate a header file for hardcoded sine windows
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ *
+ * This file is part of FFmpeg.
+ *
+ * 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.
+ *
+ * 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#define CONFIG_HARDCODED_TABLES 0
+
+#if CONFIG_FIXED
+#define ADD_SUFFIX(a) a ## _fixed
+#define INTFLOAT int
+#define WRITE_FUNC write_int32_t_array
+
+#else
+
+#define ADD_SUFFIX(a) a
+#define INTFLOAT float
+#define WRITE_FUNC write_float_array
+
+#endif
+
+#define SINETABLE_CONST
+#define SINETABLE(size) \
+ INTFLOAT ADD_SUFFIX(ff_sine_##size)[size]
+#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
+#include "sinewin_tablegen.h"
+#include "tableprint.h"
+
+int main(void)
+{
+ int i;
+
+ write_fileheader();
+
+ for (i = 5; i <= 13; i++) {
+ ADD_SUFFIX(ff_init_ff_sine_windows)(i);
+ printf("SINETABLE(%4i) = {\n", 1 << i);
+ WRITE_FUNC(ADD_SUFFIX(ff_sine_windows)[i], 1 << i);
+ printf("};\n");
+ }
+
+ return 0;
+}
diff --git a/libavcodec/tableprint.h b/libavcodec/tableprint.h
index 1b39dc6..e7c56ef 100644
--- a/libavcodec/tableprint.h
+++ b/libavcodec/tableprint.h
@@ -64,6 +64,7 @@ void write_int8_t_array (const int8_t *, int);
void write_uint8_t_array (const uint8_t *, int);
void write_uint16_t_array (const uint16_t *, int);
void write_uint32_t_array (const uint32_t *, int);
+void write_int32_t_array (const int32_t *, int);
void write_float_array (const float *, int);
void write_int8_t_2d_array (const void *, int, int);
void write_uint8_t_2d_array (const void *, int, int);
@@ -95,6 +96,7 @@ WRITE_1D_FUNC(int8_t, "%3"PRIi8, 15)
WRITE_1D_FUNC(uint8_t, "0x%02"PRIx8, 15)
WRITE_1D_FUNC(uint16_t, "0x%08"PRIx16, 7)
WRITE_1D_FUNC(uint32_t, "0x%08"PRIx32, 7)
+WRITE_1D_FUNC(int32_t, "0x%08"PRIx32, 7)
WRITE_1D_FUNC(float, "%.18e", 3)
WRITE_2D_FUNC(int8_t)
diff --git a/libavutil/fixed_dsp.c b/libavutil/fixed_dsp.c
index dcb36b0..c7ddf04 100644
--- a/libavutil/fixed_dsp.c
+++ b/libavutil/fixed_dsp.c
@@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * Author: Nedeljko Babic (nbabic at mips.com)
+ * Author: Nedeljko Babic (nedeljko.babic imgtec com)
*
* This file is part of FFmpeg.
*
@@ -47,6 +47,28 @@
#include "fixed_dsp.h"
+static void vector_fmul_add_fixed_c(int *dst, const int *src0, const int *src1, const int *src2, int len){
+ int i;
+ int64_t accu;
+
+ for (i=0; i<len; i++) {
+ accu = (int64_t)src0[i] * src1[i];
+ dst[i] = src2[i] + (int)((accu + 0x40000000) >> 31);
+ }
+}
+
+static void vector_fmul_reverse_fixed_c(int *dst, const int *src0, const int *src1, int len)
+{
+ int i;
+ int64_t accu;
+
+ src1 += len-1;
+ for (i=0; i<len; i++) {
+ accu = (int64_t)src0[i] * src1[-i];
+ dst[i] = (int)((accu+0x40000000) >> 31);
+ }
+}
+
static void vector_fmul_window_fixed_scaled_c(int16_t *dst, const int32_t *src0,
const int32_t *src1, const int32_t *win,
int len, uint8_t bits)
@@ -88,8 +110,47 @@ static void vector_fmul_window_fixed_c(int32_t *dst, const int32_t *src0,
}
}
+static void vector_fmul_fixed_c(int *dst, const int *src0, const int *src1, int len)
+{
+ int i;
+ int64_t accu;
+
+ for (i = 0; i < len; i++){
+ accu = (int64_t)src0[i] * src1[i];
+ dst[i] = (int)((accu+0x40000000) >> 31);
+ }
+}
+
+static int ff_scalarproduct_fixed_c(const int *v1, const int *v2, int len)
+{
+ int64_t p = 0;
+ int i;
+
+ for (i = 0; i < len; i++)
+ p += (int64_t)v1[i] * v2[i];
+
+ return (int)((p + 0x40000000) >> 31);
+}
+
+static void butterflies_fixed_c(int *v1, int *v2, int len)
+{
+ int i;
+
+ for (i = 0; i < len; i++){
+ int t = v1[i] - v2[i];
+ v1[i] += v2[i];
+ v2[i] = t;
+ }
+}
+
void avpriv_fixed_dsp_init(AVFixedDSPContext *fdsp, int bit_exact)
{
+ fdsp->vector_fmul_fixed = vector_fmul_fixed_c;
+ fdsp->vector_fmul_add_fixed = vector_fmul_add_fixed_c;
+ fdsp->vector_fmul_reverse_fixed = vector_fmul_reverse_fixed_c;
fdsp->vector_fmul_window_fixed_scaled = vector_fmul_window_fixed_scaled_c;
fdsp->vector_fmul_window_fixed = vector_fmul_window_fixed_c;
+ fdsp->vector_fmul_fixed = vector_fmul_fixed_c;
+ fdsp->butterflies_fixed = butterflies_fixed_c;
+ fdsp->scalarproduct_fixed = ff_scalarproduct_fixed_c;
}
diff --git a/libavutil/fixed_dsp.h b/libavutil/fixed_dsp.h
index 85f5252..20d4095 100644
--- a/libavutil/fixed_dsp.h
+++ b/libavutil/fixed_dsp.h
@@ -53,6 +53,25 @@
#include "common.h"
typedef struct AVFixedDSPContext {
+ /* assume len is a multiple of 16, and arrays are 32-byte aligned */
+
+ /**
+ * Calculate the product of two vectors of integers and store the result in
+ * a vector of integers.
+ *
+ * @param dst output vector
+ * constraints: 32-byte aligned
+ * @param src0 first input vector
+ * constraints: 32-byte aligned
+ * @param src1 second input vector
+ * constraints: 32-byte aligned
+ * @param len number of elements in the input
+ * constraints: multiple of 16
+ */
+ void (*vector_fmul_fixed)(int *dst, const int *src0, const int *src1,
+ int len);
+
+ void (*vector_fmul_reverse_fixed)(int *dst, const int *src0, const int *src1, int len);
/**
* Overlap/add with window function.
* Used primarily by MDCT-based audio codecs.
@@ -91,6 +110,40 @@ typedef struct AVFixedDSPContext {
*/
void (*vector_fmul_window_fixed)(int32_t *dst, const int32_t *src0, const int32_t *src1, const int32_t *win, int len);
+ /**
+ * Calculate the product of two vectors of integers, add a third vector of
+ * integers and store the result in a vector of integers.
+ *
+ * @param dst output vector
+ * constraints: 32-byte aligned
+ * @param src0 first input vector
+ * constraints: 32-byte aligned
+ * @param src1 second input vector
+ * constraints: 32-byte aligned
+ * @param src1 third input vector
+ * constraints: 32-byte aligned
+ * @param len number of elements in the input
+ * constraints: multiple of 16
+ */
+ void (*vector_fmul_add_fixed)(int *dst, const int *src0, const int *src1,
+ const int *src2, int len);
+
+ /**
+ * Calculate the scalar product of two vectors of floats.
+ * @param v1 first vector, 16-byte aligned
+ * @param v2 second vector, 16-byte aligned
+ * @param len length of vectors, multiple of 4
+ */
+ int (*scalarproduct_fixed)(const int *v1, const int *v2, int len);
+
+ /**
+ * Calculate the sum and difference of two vectors of integers.
+ *
+ * @param v1 first input vector, sum output, 16-byte aligned
+ * @param v2 second input vector, difference output, 16-byte aligned
+ * @param len length of vectors, multiple of 4
+ */
+ void (*butterflies_fixed)(int *av_restrict v1, int *av_restrict v2, int len);
} AVFixedDSPContext;
/**
--
1.7.3.4
More information about the ffmpeg-devel
mailing list