Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,8 @@
clean::
$(RM) $(CLEANSUFFIXES:%=libavcodec/aac/%)
OBJS-$(CONFIG_AAC_DECODER) += aac/aacdec.o aac/aacdec_tab.o \
aac/aacdec_float.o aac/aacdec_usac.o \
aac/aacdec_ac.o aac/aacdec_lpd.o
OBJS-$(CONFIG_AAC_FIXED_DECODER) += aac/aacdec.o aac/aacdec_tab.o \
aac/aacdec_fixed.o
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,562 @@
/*
* AAC decoder definitions and structures
* 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 definitions and structures
* @author Oded Shimon ( ods15 ods15 dyndns org )
* @author Maxim Gavrilov ( maxim.gavrilov gmail com )
*/
#ifndef AVCODEC_AAC_AACDEC_H
#define AVCODEC_AAC_AACDEC_H
#include <stdint.h>
#include "libavutil/channel_layout.h"
#include "libavutil/float_dsp.h"
#include "libavutil/fixed_dsp.h"
#include "libavutil/mem_internal.h"
#include "libavutil/tx.h"
#include "libavcodec/aac.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/mpeg4audio.h"
#include "aacdec_ac.h"
typedef struct AACDecContext AACDecContext;
/**
* Output configuration status
*/
enum OCStatus {
OC_NONE, ///< Output unconfigured
OC_TRIAL_PCE, ///< Output configuration under trial specified by an inband PCE
OC_TRIAL_FRAME, ///< Output configuration under trial specified by a frame header
OC_GLOBAL_HDR, ///< Output configuration set in a global header but not yet locked
OC_LOCKED, ///< Output configuration locked in place
};
enum AACOutputChannelOrder {
CHANNEL_ORDER_DEFAULT,
CHANNEL_ORDER_CODED,
};
/**
* The point during decoding at which channel coupling is applied.
*/
enum CouplingPoint {
BEFORE_TNS,
BETWEEN_TNS_AND_IMDCT,
AFTER_IMDCT = 3,
};
enum AACUsacElem {
ID_USAC_SCE = 0,
ID_USAC_CPE = 1,
ID_USAC_LFE = 2,
ID_USAC_EXT = 3,
};
enum ExtensionHeaderType {
ID_CONFIG_EXT_FILL = 0,
ID_CONFIG_EXT_LOUDNESS_INFO = 2,
ID_CONFIG_EXT_STREAM_ID = 7,
};
enum AACUsacExtension {
ID_EXT_ELE_FILL,
ID_EXT_ELE_MPEGS,
ID_EXT_ELE_SAOC,
ID_EXT_ELE_AUDIOPREROLL,
ID_EXT_ELE_UNI_DRC,
};
enum AACUSACLoudnessExt {
UNIDRCLOUDEXT_TERM = 0x0,
UNIDRCLOUDEXT_EQ = 0x1,
};
// Supposed to be equal to AAC_RENAME() in case of USE_FIXED.
#define RENAME_FIXED(name) name ## _fixed
#define INTFLOAT_UNION(name, elems) \
union { \
int RENAME_FIXED(name) elems; \
float name elems; \
}
#define INTFLOAT_ALIGNED_UNION(alignment, name, nb_elems) \
union { \
DECLARE_ALIGNED(alignment, int, RENAME_FIXED(name))[nb_elems]; \
DECLARE_ALIGNED(alignment, float, name)[nb_elems]; \
}
/**
* Long Term Prediction
*/
typedef struct LongTermPrediction {
int8_t present;
int16_t lag;
INTFLOAT_UNION(coef,);
int8_t used[MAX_LTP_LONG_SFB];
} LongTermPrediction;
/* Per channel core mode */
typedef struct AACUsacElemData {
uint8_t core_mode;
uint8_t scale_factor_grouping;
uint8_t tns_data_present;
/* Timewarping ratio */
#define NUM_TW_NODES 16
uint8_t tw_ratio[NUM_TW_NODES];
struct {
uint8_t acelp_core_mode : 3;
uint8_t lpd_mode : 5;
uint8_t bpf_control_info : 1;
uint8_t core_mode_last : 1;
uint8_t fac_data_present : 1;
int last_lpd_mode;
} ldp;
struct {
unsigned int seed;
uint8_t level : 3;
uint8_t offset : 5;
} noise;
struct {
uint8_t gain;
uint32_t kv[8 /* (1024 / 16) / 8 */][8];
} fac;
AACArithState ac;
} AACUsacElemData;
/**
* Individual Channel Stream
*/
typedef struct IndividualChannelStream {
uint8_t max_sfb; ///< number of scalefactor bands per group
enum WindowSequence window_sequence[2];
uint8_t use_kb_window[2]; ///< If set, use Kaiser-Bessel window, otherwise use a sine window.
int num_window_groups;
int prev_num_window_groups; ///< Previous frame's number of window groups
uint8_t group_len[8];
LongTermPrediction ltp;
const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
int num_swb; ///< number of scalefactor window bands
int num_windows;
int tns_max_bands;
int predictor_present;
int predictor_initialized;
int predictor_reset_group;
uint8_t prediction_used[41];
uint8_t window_clipping[8]; ///< set if a certain window is near clipping
} IndividualChannelStream;
/**
* Temporal Noise Shaping
*/
typedef struct TemporalNoiseShaping {
int present;
int n_filt[8];
int length[8][4];
int direction[8][4];
int order[8][4];
INTFLOAT_UNION(coef, [8][4][TNS_MAX_ORDER]);
} TemporalNoiseShaping;
/**
* coupling parameters
*/
typedef struct ChannelCoupling {
enum CouplingPoint coupling_point; ///< The point during decoding at which coupling is applied.
int num_coupled; ///< number of target elements
enum RawDataBlockType type[8]; ///< Type of channel element to be coupled - SCE or CPE.
int id_select[8]; ///< element id
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
*/
INTFLOAT_UNION(gain, [16][120]);
} ChannelCoupling;
/**
* Single Channel Element - used for both SCE and LFE elements.
*/
typedef struct SingleChannelElement {
IndividualChannelStream ics;
AACUsacElemData ue; ///< USAC element data
TemporalNoiseShaping tns;
enum BandType band_type[128]; ///< band types
int sfo[128]; ///< scalefactor offsets
INTFLOAT_UNION(sf, [128]); ///< scalefactors (8 windows * 16 sfb max)
INTFLOAT_ALIGNED_UNION(32, coeffs, 1024); ///< coefficients for IMDCT, maybe processed
INTFLOAT_ALIGNED_UNION(32, prev_coeffs, 1024); ///< unscaled previous contents of coeffs[] for USAC
INTFLOAT_ALIGNED_UNION(32, saved, 1536); ///< overlap
INTFLOAT_ALIGNED_UNION(32, ret_buf, 2048); ///< PCM output buffer
INTFLOAT_ALIGNED_UNION(16, ltp_state, 3072); ///< time signal for LTP
union {
struct PredictorStateFixed *RENAME_FIXED(predictor_state);
struct PredictorState *predictor_state;
};
union {
float *output; ///< PCM output
int *RENAME_FIXED(output); ///< PCM output
};
} SingleChannelElement;
typedef struct AACUsacStereo {
uint8_t common_window;
uint8_t common_tw;
uint8_t tns_on_lr; ///< Apply TNS before M/S and stereo prediction
uint8_t ms_mask_mode;
uint8_t config_idx;
/* Complex prediction */
uint8_t use_prev_frame;
uint8_t pred_dir;
uint8_t complex_coef;
uint8_t pred_used[128];
INTFLOAT_ALIGNED_UNION(32, alpha_q_re, 1024);
INTFLOAT_ALIGNED_UNION(32, alpha_q_im, 1024);
INTFLOAT_ALIGNED_UNION(32, prev_alpha_q_re, 1024);
INTFLOAT_ALIGNED_UNION(32, prev_alpha_q_im, 1024);
INTFLOAT_ALIGNED_UNION(32, dmix_re, 1024);
INTFLOAT_ALIGNED_UNION(32, prev_dmix_re, 1024); /* Recalculated on every frame */
INTFLOAT_ALIGNED_UNION(32, dmix_im, 1024); /* Final prediction data */
} AACUsacStereo;
/**
* channel element - generic struct for SCE/CPE/CCE/LFE
*/
typedef struct ChannelElement {
int present;
// CPE specific
uint8_t max_sfb_ste; ///< (USAC) Maximum of both max_sfb values
uint8_t ms_mask[128]; ///< Set if mid/side stereo is used for each scalefactor window band
// shared
SingleChannelElement ch[2];
// CCE specific
ChannelCoupling coup;
// USAC stereo coupling data
AACUsacStereo us;
} ChannelElement;
typedef struct AACUSACLoudnessInfo {
uint8_t drc_set_id : 6;
uint8_t downmix_id : 7;
struct {
uint16_t lvl : 12;
uint8_t present : 1;
} sample_peak;
struct {
uint16_t lvl : 12;
uint8_t measurement : 4;
uint8_t reliability : 2;
uint8_t present : 1;
} true_peak;
uint8_t nb_measurements : 4;
struct {
uint8_t method_def : 4;
uint8_t method_val;
uint8_t measurement : 4;
uint8_t reliability : 2;
} measurements[16];
} AACUSACLoudnessInfo;
typedef struct AACUsacElemConfig {
enum AACUsacElem type;
uint8_t tw_mdct : 1;
uint8_t noise_fill : 1;
uint8_t stereo_config_index;
struct {
int ratio;
uint8_t harmonic_sbr : 1; /* harmonicSBR */
uint8_t bs_intertes : 1; /* bs_interTes */
uint8_t bs_pvc : 1; /* bs_pvc */
struct {
uint8_t start_freq; /* dflt_start_freq */
uint8_t stop_freq; /* dflt_stop_freq */
uint8_t freq_scale; /* dflt_freq_scale */
uint8_t alter_scale : 1; /* dflt_alter_scale */
uint8_t noise_bands; /* dflt_noise_bands */
uint8_t limiter_bands; /* dflt_limiter_bands */
uint8_t limiter_gains; /* dflt_limiter_gains */
uint8_t interpol_freq : 1; /* dflt_interpol_freq */
uint8_t smoothing_mode : 1; /* dflt_smoothing_mode */
} dflt;
} sbr;
struct {
uint8_t freq_res; /* bsFreqRes */
uint8_t fixed_gain; /* bsFixedGainDMX */
uint8_t temp_shape_config; /* bsTempShapeConfig */
uint8_t decorr_config; /* bsDecorrConfig */
uint8_t high_rate_mode : 1; /* bsHighRateMode */
uint8_t phase_coding : 1; /* bsPhaseCoding */
uint8_t otts_bands_phase; /* bsOttBandsPhase */
uint8_t residual_coding; /* bsResidualCoding */
uint8_t residual_bands; /* bsResidualBands */
uint8_t pseudo_lr : 1; /* bsPseudoLr */
uint8_t env_quant_mode : 1; /* bsEnvQuantMode */
} mps;
struct {
enum AACUsacExtension type;
uint8_t payload_frag;
uint32_t default_len;
uint32_t pl_data_offset;
uint8_t *pl_data;
} ext;
} AACUsacElemConfig;
typedef struct AACUSACConfig {
uint8_t core_sbr_frame_len_idx; /* coreSbrFrameLengthIndex */
uint16_t core_frame_len;
uint16_t stream_identifier;
AACUsacElemConfig elems[64];
int nb_elems;
struct {
uint8_t nb_album;
AACUSACLoudnessInfo album_info[64];
uint8_t nb_info;
AACUSACLoudnessInfo info[64];
} loudness;
} AACUSACConfig;
typedef struct OutputConfiguration {
MPEG4AudioConfig m4ac;
uint8_t layout_map[MAX_ELEM_ID*4][3];
int layout_map_tags;
AVChannelLayout ch_layout;
enum OCStatus status;
AACUSACConfig usac;
} OutputConfiguration;
/**
* Dynamic Range Control - decoded from the bitstream but not processed further.
*/
typedef struct DynamicRangeControl {
int pce_instance_tag; ///< Indicates with which program the DRC info is associated.
int dyn_rng_sgn[17]; ///< DRC sign information; 0 - positive, 1 - negative
int dyn_rng_ctl[17]; ///< DRC magnitude information
int exclude_mask[MAX_CHANNELS]; ///< Channels to be excluded from DRC processing.
int band_incr; ///< Number of DRC bands greater than 1 having DRC info.
int interpolation_scheme; ///< Indicates the interpolation scheme used in the SBR QMF domain.
int band_top[17]; ///< Indicates the top of the i-th DRC band in units of 4 spectral lines.
int prog_ref_level; /**< A reference level for the long-term program audio level for all
* channels combined.
*/
} DynamicRangeControl;
/**
* Decode-specific primitives
*/
typedef struct AACDecProc {
int (*decode_spectrum_and_dequant)(AACDecContext *ac,
GetBitContext *gb,
const Pulse *pulse,
SingleChannelElement *sce);
int (*decode_cce)(AACDecContext *ac, GetBitContext *gb, ChannelElement *che);
int (*sbr_ctx_alloc_init)(AACDecContext *ac, ChannelElement **che, int id_aac);
int (*sbr_decode_extension)(AACDecContext *ac, ChannelElement *che,
GetBitContext *gb, int crc, int cnt, int id_aac);
void (*sbr_apply)(AACDecContext *ac, ChannelElement *che,
int id_aac, void /* INTFLOAT */ *L, void /* INTFLOAT */ *R);
void (*sbr_ctx_close)(ChannelElement *che);
} AACDecProc;
/**
* DSP-specific primitives
*/
typedef struct AACDecDSP {
void (*dequant_scalefactors)(SingleChannelElement *sce);
void (*apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe);
void (*apply_intensity_stereo)(AACDecContext *ac, ChannelElement *cpe,
int ms_present);
void (*apply_tns)(void *_coef_param, TemporalNoiseShaping *tns,
IndividualChannelStream *ics, int decode);
void (*apply_ltp)(AACDecContext *ac, SingleChannelElement *sce);
void (*update_ltp)(AACDecContext *ac, SingleChannelElement *sce);
void (*apply_prediction)(AACDecContext *ac, SingleChannelElement *sce);
void (*apply_dependent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index);
void (*apply_independent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index);
void (*imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce);
void (*imdct_and_windowing_768)(AACDecContext *ac, SingleChannelElement *sce);
void (*imdct_and_windowing_960)(AACDecContext *ac, SingleChannelElement *sce);
void (*imdct_and_windowing_ld)(AACDecContext *ac, SingleChannelElement *sce);
void (*imdct_and_windowing_eld)(AACDecContext *ac, SingleChannelElement *sce);
void (*clip_output)(AACDecContext *ac, ChannelElement *che, int type, int samples);
} AACDecDSP;
/**
* main AAC decoding context
*/
struct AACDecContext {
const struct AVClass *class;
struct AVCodecContext *avctx;
AACDecDSP dsp;
AACDecProc proc;
struct AVFrame *frame;
int is_saved; ///< Set if elements have stored overlap from previous frame.
DynamicRangeControl che_drc;
/**
* @name Channel element related data
* @{
*/
ChannelElement *che[4][MAX_ELEM_ID];
ChannelElement *tag_che_map[4][MAX_ELEM_ID];
int tags_mapped;
int warned_remapping_once;
/** @} */
/**
* @name temporary aligned temporary buffers
* (We do not want to have these on the stack.)
* @{
*/
INTFLOAT_ALIGNED_UNION(32, buf_mdct, 1024);
INTFLOAT_ALIGNED_UNION(32, temp, 128);
/** @} */
/**
* @name Computed / set up during initialization
* @{
*/
AVTXContext *mdct96;
AVTXContext *mdct120;
AVTXContext *mdct128;
AVTXContext *mdct480;
AVTXContext *mdct512;
AVTXContext *mdct768;
AVTXContext *mdct960;
AVTXContext *mdct1024;
AVTXContext *mdct_ltp;
av_tx_fn mdct96_fn;
av_tx_fn mdct120_fn;
av_tx_fn mdct128_fn;
av_tx_fn mdct480_fn;
av_tx_fn mdct512_fn;
av_tx_fn mdct768_fn;
av_tx_fn mdct960_fn;
av_tx_fn mdct1024_fn;
av_tx_fn mdct_ltp_fn;
union {
AVFixedDSPContext *RENAME_FIXED(fdsp);
AVFloatDSPContext *fdsp;
};
int random_state;
/** @} */
/**
* @name Members used for output
* @{
*/
SingleChannelElement *output_element[MAX_CHANNELS]; ///< Points to each SingleChannelElement
/** @} */
/**
* @name Japanese DTV specific extension
* @{
*/
int force_dmono_mode;///< 0->not dmono, 1->use first channel, 2->use second channel
int dmono_mode; ///< 0->not dmono, 1->use first channel, 2->use second channel
/** @} */
enum AACOutputChannelOrder output_channel_order;
OutputConfiguration oc[2];
int warned_num_aac_frames;
int warned_960_sbr;
unsigned warned_71_wide;
int warned_gain_control;
int warned_he_aac_mono;
int is_fixed;
};
#if defined(USE_FIXED) && USE_FIXED
#define fdsp RENAME_FIXED(fdsp)
#endif
int ff_aac_decode_init(AVCodecContext *avctx);
int ff_aac_decode_init_float(AVCodecContext *avctx);
int ff_aac_decode_init_fixed(AVCodecContext *avctx);
int ff_aac_decode_ics(AACDecContext *ac, SingleChannelElement *sce,
GetBitContext *gb, int common_window, int scale_flag);
int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns,
GetBitContext *gb, const IndividualChannelStream *ics);
int ff_aac_set_default_channel_config(AACDecContext *ac, AVCodecContext *avctx,
uint8_t (*layout_map)[3],
int *tags,
int channel_config);
int ff_aac_output_configure(AACDecContext *ac,
uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
enum OCStatus oc_type, int get_new_frame);
ChannelElement *ff_aac_get_che(AACDecContext *ac, int type, int elem_id);
#endif /* AVCODEC_AAC_AACDEC_H */
@@ -0,0 +1,208 @@
/*
* AAC definitions and structures
* Copyright (c) 2024 Lynne
*
* 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 "libavcodec/aactab.h"
#include "aacdec_ac.h"
uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
{
float ratio;
if (reset) {
memset(state->last, 0, sizeof(state->last));
state->last_len = N;
} else if (state->last_len != N) {
int i;
uint8_t last[512 /* 2048 / 4 */];
memcpy(last, state->last, sizeof(last));
ratio = state->last_len / (float)N;
for (i = 0; i < N/2; i++) {
int k = (int)(i * ratio);
state->last[i] = last[k];
}
for (; i < FF_ARRAY_ELEMS(state->last); i++)
state->last[i] = 0;
state->last_len = N;
}
state->cur[3] = 0;
state->cur[2] = 0;
state->cur[1] = 0;
state->cur[0] = 1;
state->state_pre = state->last[0] << 12;
return state->last[0] << 12;
}
uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
{
c = state->state_pre >> 8;
c = c + (state->last[i + 1] << 8);
c = (c << 4);
c += state->cur[1];
state->state_pre = c;
if (i > 3 &&
((state->cur[3] + state->cur[2] + state->cur[1]) < 5))
return c + 0x10000;
return c;
}
uint32_t ff_aac_ac_get_pk(uint32_t c)
{
int i_min = -1;
int i, j;
int i_max = FF_ARRAY_ELEMS(ff_aac_ac_lookup_m) - 1;
while ((i_max - i_min) > 1) {
i = i_min + ((i_max - i_min) / 2);
j = ff_aac_ac_hash_m[i];
if (c < (j >> 8))
i_max = i;
else if (c > (j >> 8))
i_min = i;
else
return (j & 0xFF);
}
return ff_aac_ac_lookup_m[i_max];
}
void ff_aac_ac_update_context(AACArithState *state, int idx,
uint16_t a, uint16_t b)
{
state->cur[0] = a + b + 1;
if (state->cur[0] > 0xF)
state->cur[0] = 0xF;
state->cur[3] = state->cur[2];
state->cur[2] = state->cur[1];
state->cur[1] = state->cur[0];
state->last[idx] = state->cur[0];
}
/* Initialize AC */
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
{
ac->low = 0;
ac->high = UINT16_MAX;
ac->val = get_bits(gb, 16);
}
uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb,
const uint16_t *cdf, uint16_t cdf_len)
{
int val = ac->val;
int low = ac->low;
int high = ac->high;
int sym;
int rng = high - low + 1;
int c = ((((int)(val - low + 1)) << 14) - ((int)1));
const uint16_t *p = cdf - 1;
/* One for each possible CDF length in the spec */
switch (cdf_len) {
case 2:
if ((p[1] * rng) > c)
p += 1;
break;
case 4:
if ((p[2] * rng) > c)
p += 2;
if ((p[1] * rng) > c)
p += 1;
break;
case 17:
/* First check if the current probability is even met at all */
if ((p[1] * rng) <= c)
break;
p += 1;
for (int i = 8; i >= 1; i >>= 1)
if ((p[i] * rng) > c)
p += i;
break;
case 27:
if ((p[16] * rng) > c)
p += 16;
if ((p[8] * rng) > c)
p += 8;
if (p != (cdf - 1 + 24))
if ((p[4] * rng) > c)
p += 4;
if ((p[2] * rng) > c)
p += 2;
if (p != (cdf - 1 + 24 + 2))
if ((p[1] * rng) > c)
p += 1;
break;
default:
/* This should never happen */
av_assert2(0);
}
sym = (int)((ptrdiff_t)(p - cdf)) + 1;
if (sym)
high = low + ((rng * cdf[sym - 1]) >> 14) - 1;
low += (rng * cdf[sym]) >> 14;
/* This loop could be done faster */
while (1) {
if (high < 32768) {
;
} else if (low >= 32768) {
val -= 32768;
low -= 32768;
high -= 32768;
} else if (low >= 16384 && high < 49152) {
val -= 16384;
low -= 16384;
high -= 16384;
} else {
break;
}
low += low;
high += high + 1;
val = (val << 1) | get_bits1(gb);
};
ac->low = low;
ac->high = high;
ac->val = val;
return sym;
}
void ff_aac_ac_finish(AACArithState *state, int offset, int N)
{
int i;
for (i = offset; i < N/2; i++)
state->last[i] = 1;
for (; i < FF_ARRAY_ELEMS(state->last); i++)
state->last[i] = 0;
}
@@ -0,0 +1,54 @@
/*
* AAC definitions and structures
* Copyright (c) 2024 Lynne
*
* 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 AVCODEC_AAC_AACDEC_AC_H
#define AVCODEC_AAC_AACDEC_AC_H
#include "libavcodec/get_bits.h"
typedef struct AACArithState {
uint8_t last[512 /* 2048 / 4 */ + 1];
int last_len;
uint8_t cur[4];
uint16_t state_pre;
} AACArithState;
typedef struct AACArith {
uint16_t low;
uint16_t high;
uint16_t val;
} AACArith;
#define FF_AAC_AC_ESCAPE 16
uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int len);
uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t old_c, int idx, int len);
uint32_t ff_aac_ac_get_pk(uint32_t c);
void ff_aac_ac_update_context(AACArithState *state, int idx, uint16_t a, uint16_t b);
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb);
uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb,
const uint16_t *cdf, uint16_t cdf_len);
void ff_aac_ac_finish(AACArithState *state, int offset, int nb);
#endif /* AVCODEC_AACDEC_AC_H */
@@ -0,0 +1,688 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#include "aacdec.h"
#include "libavcodec/lpc_functions.h"
#include "libavcodec/aactab.h"
/**
* Convert integer scalefactors to the decoder's native expected
* scalefactor values.
*/
static void AAC_RENAME(dequant_scalefactors)(SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
const int *sfo = sce->sfo;
INTFLOAT *sf = sce->AAC_RENAME(sf);
int idx = 0;
for (int g = 0; g < ics->num_window_groups; g++) {
for (int sfb = 0; sfb < ics->max_sfb; sfb++, idx++) {
switch (sce->band_type[g*ics->max_sfb + sfb]) {
case ZERO_BT:
sf[idx] = FIXR(0.);
break;
case INTENSITY_BT: /* fallthrough */
case INTENSITY_BT2:
#if USE_FIXED
sf[idx] = 100 - (sfo[idx] + 100);
#else
sf[idx] = ff_aac_pow2sf_tab[-sfo[idx] - 100 + POW_SF2_ZERO];
#endif /* USE_FIXED */
break;
case NOISE_BT:
#if USE_FIXED
sf[idx] = -(100 + sfo[idx]);
#else
sf[idx] = -ff_aac_pow2sf_tab[sfo[idx] + POW_SF2_ZERO];
#endif /* USE_FIXED */
break;
default:
#if USE_FIXED
sf[idx] = -sfo[idx] - 100;
#else
sf[idx] = -ff_aac_pow2sf_tab[sfo[idx] + POW_SF2_ZERO];
#endif /* USE_FIXED */
break;
}
}
}
}
/**
* Mid/Side stereo decoding; reference: 4.6.8.1.3.
*/
static void AAC_RENAME(apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
{
const IndividualChannelStream *ics = &cpe->ch[0].ics;
INTFLOAT *ch0 = cpe->ch[0].AAC_RENAME(coeffs);
INTFLOAT *ch1 = cpe->ch[1].AAC_RENAME(coeffs);
const uint16_t *offsets = ics->swb_offset;
for (int g = 0; g < ics->num_window_groups; g++) {
for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
const int idx = g*cpe->max_sfb_ste + sfb;
if (cpe->ms_mask[idx] &&
cpe->ch[0].band_type[idx] < NOISE_BT &&
cpe->ch[1].band_type[idx] < NOISE_BT) {
for (int group = 0; group < ics->group_len[g]; group++)
#if USE_FIXED
ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[sfb],
ch1 + group * 128 + offsets[sfb],
offsets[sfb+1] - offsets[sfb]);
#else
ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[sfb],
ch1 + group * 128 + offsets[sfb],
offsets[sfb+1] - offsets[sfb]);
#endif /* USE_FIXED */
}
}
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 AAC_RENAME(apply_intensity_stereo)(AACDecContext *ac,
ChannelElement *cpe, int ms_present)
{
const IndividualChannelStream *ics = &cpe->ch[1].ics;
SingleChannelElement *sce1 = &cpe->ch[1];
INTFLOAT *coef0 = cpe->ch[0].AAC_RENAME(coeffs), *coef1 = cpe->ch[1].AAC_RENAME(coeffs);
const uint16_t *offsets = ics->swb_offset;
int c;
INTFLOAT scale;
for (int g = 0; g < ics->num_window_groups; g++) {
for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
const int idx = g*ics->max_sfb + sfb;
if (sce1->band_type[idx] == INTENSITY_BT ||
sce1->band_type[idx] == INTENSITY_BT2) {
c = -1 + 2 * (sce1->band_type[idx] - 14);
if (ms_present)
c *= 1 - 2 * cpe->ms_mask[idx];
scale = c * sce1->AAC_RENAME(sf)[idx];
for (int group = 0; group < ics->group_len[g]; group++)
#if USE_FIXED
subband_scale(coef1 + group * 128 + offsets[sfb],
coef0 + group * 128 + offsets[sfb],
scale,
23,
offsets[sfb + 1] - offsets[sfb], ac->avctx);
#else
ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[sfb],
coef0 + group * 128 + offsets[sfb],
scale,
offsets[sfb + 1] - offsets[sfb]);
#endif /* USE_FIXED */
}
}
coef0 += ics->group_len[g] * 128;
coef1 += ics->group_len[g] * 128;
}
}
/**
* 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 AAC_RENAME(apply_tns)(void *_coef_param, 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 *coef_param = _coef_param;
INTFLOAT lpc[TNS_MAX_ORDER];
INTFLOAT tmp[TNS_MAX_ORDER+1];
UINTFLOAT *coef = coef_param;
if(!mmm)
return;
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->AAC_RENAME(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((INTFLOAT)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 inline void AAC_RENAME(windowing_and_mdct_ltp)(AACDecContext *ac,
INTFLOAT *out, INTFLOAT *in,
IndividualChannelStream *ics)
{
const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(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(*in));
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(*in));
}
ac->mdct_ltp_fn(ac->mdct_ltp, out, in, sizeof(INTFLOAT));
}
/**
* Apply the long term prediction
*/
static void AAC_RENAME(apply_ltp)(AACDecContext *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->AAC_RENAME(output);
INTFLOAT *predFreq = ac->AAC_RENAME(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->AAC_RENAME(ltp_state)[i + 2048 - ltp->lag], ltp->AAC_RENAME(coef));
memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
AAC_RENAME(windowing_and_mdct_ltp)(ac, predFreq, predTime, &sce->ics);
if (sce->tns.present)
AAC_RENAME(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->AAC_RENAME(coeffs)[i] += (UINTFLOAT)predFreq[i];
}
}
/**
* Update the LTP buffer for next frame
*/
static void AAC_RENAME(update_ltp)(AACDecContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
INTFLOAT *saved = sce->AAC_RENAME(saved);
INTFLOAT *saved_ltp = sce->AAC_RENAME(coeffs);
const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
int i;
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
memcpy(saved_ltp, saved, 512 * sizeof(*saved_ltp));
memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->AAC_RENAME(buf_mdct) + 960, &swindow[64], 64);
for (i = 0; i < 64; i++)
saved_ltp[i + 512] = AAC_MUL31(ac->AAC_RENAME(buf_mdct)[1023 - i], swindow[63 - i]);
} else if (1 && ics->window_sequence[0] == LONG_START_SEQUENCE) {
memcpy(saved_ltp, ac->AAC_RENAME(buf_mdct) + 512, 448 * sizeof(*saved_ltp));
memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->AAC_RENAME(buf_mdct) + 960, &swindow[64], 64);
for (i = 0; i < 64; i++)
saved_ltp[i + 512] = AAC_MUL31(ac->AAC_RENAME(buf_mdct)[1023 - i], swindow[63 - i]);
} else if (1) { // LONG_STOP or ONLY_LONG
ac->fdsp->vector_fmul_reverse(saved_ltp, ac->AAC_RENAME(buf_mdct) + 512, &lwindow[512], 512);
for (i = 0; i < 512; i++)
saved_ltp[i + 512] = AAC_MUL31(ac->AAC_RENAME(buf_mdct)[1023 - i], lwindow[511 - i]);
}
memcpy(sce->AAC_RENAME(ltp_state), sce->AAC_RENAME(ltp_state)+1024,
1024 * sizeof(*sce->AAC_RENAME(ltp_state)));
memcpy(sce->AAC_RENAME(ltp_state) + 1024, sce->AAC_RENAME(output),
1024 * sizeof(*sce->AAC_RENAME(ltp_state)));
memcpy(sce->AAC_RENAME(ltp_state) + 2048, saved_ltp,
1024 * sizeof(*sce->AAC_RENAME(ltp_state)));
}
/**
* Conduct IMDCT and windowing.
*/
static void AAC_RENAME(imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
INTFLOAT *in = sce->AAC_RENAME(coeffs);
INTFLOAT *out = sce->AAC_RENAME(output);
INTFLOAT *saved = sce->AAC_RENAME(saved);
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
INTFLOAT *buf = ac->AAC_RENAME(buf_mdct);
INTFLOAT *temp = ac->AAC_RENAME(temp);
int i;
// imdct
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
for (i = 0; i < 1024; i += 128)
ac->mdct128_fn(ac->mdct128, buf + i, in + i, sizeof(INTFLOAT));
} else {
ac->mdct1024_fn(ac->mdct1024, buf, in, sizeof(INTFLOAT));
}
/* 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(*out));
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(*out));
} else {
ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
memcpy( out + 576, buf + 64, 448 * sizeof(*out));
}
}
// buffer update
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
memcpy( saved, temp + 64, 64 * sizeof(*saved));
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(*saved));
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
memcpy( saved, buf + 512, 448 * sizeof(*saved));
memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
} else { // LONG_STOP or ONLY_LONG
memcpy( saved, buf + 512, 512 * sizeof(*saved));
}
}
/**
* Conduct IMDCT and windowing for 768-point frames.
*/
static void AAC_RENAME(imdct_and_windowing_768)(AACDecContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
INTFLOAT *in = sce->AAC_RENAME(coeffs);
INTFLOAT *out = sce->AAC_RENAME(output);
INTFLOAT *saved = sce->AAC_RENAME(saved);
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(aac_kbd_short_96) : AAC_RENAME(sine_96);
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_long_768) : AAC_RENAME(sine_768);
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_short_96) : AAC_RENAME(sine_96);
INTFLOAT *buf = ac->AAC_RENAME(buf_mdct);
INTFLOAT *temp = ac->AAC_RENAME(temp);
int i;
// imdct
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
for (i = 0; i < 8; i++)
ac->mdct96_fn(ac->mdct96, buf + i * 96, in + i * 96, sizeof(INTFLOAT));
} else {
ac->mdct768_fn(ac->mdct768, buf, in, sizeof(INTFLOAT));
}
/* 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, 384);
} else {
memcpy( out, saved, 336 * sizeof(*out));
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
ac->fdsp->vector_fmul_window(out + 336 + 0*96, saved + 336, buf + 0*96, swindow_prev, 48);
ac->fdsp->vector_fmul_window(out + 336 + 1*96, buf + 0*96 + 48, buf + 1*96, swindow, 48);
ac->fdsp->vector_fmul_window(out + 336 + 2*96, buf + 1*96 + 48, buf + 2*96, swindow, 48);
ac->fdsp->vector_fmul_window(out + 336 + 3*96, buf + 2*96 + 48, buf + 3*96, swindow, 48);
ac->fdsp->vector_fmul_window(temp, buf + 3*96 + 48, buf + 4*96, swindow, 48);
memcpy( out + 336 + 4*96, temp, 48 * sizeof(*out));
} else {
ac->fdsp->vector_fmul_window(out + 336, saved + 336, buf, swindow_prev, 48);
memcpy( out + 432, buf + 48, 336 * sizeof(*out));
}
}
// buffer update
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
memcpy( saved, temp + 48, 48 * sizeof(*saved));
ac->fdsp->vector_fmul_window(saved + 48, buf + 4*96 + 48, buf + 5*96, swindow, 48);
ac->fdsp->vector_fmul_window(saved + 144, buf + 5*96 + 48, buf + 6*96, swindow, 48);
ac->fdsp->vector_fmul_window(saved + 240, buf + 6*96 + 48, buf + 7*96, swindow, 48);
memcpy( saved + 336, buf + 7*96 + 48, 48 * sizeof(*saved));
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
memcpy( saved, buf + 384, 336 * sizeof(*saved));
memcpy( saved + 336, buf + 7*96 + 48, 48 * sizeof(*saved));
} else { // LONG_STOP or ONLY_LONG
memcpy( saved, buf + 384, 384 * sizeof(*saved));
}
}
/**
* Conduct IMDCT and windowing.
*/
static void AAC_RENAME(imdct_and_windowing_960)(AACDecContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
INTFLOAT *in = sce->AAC_RENAME(coeffs);
INTFLOAT *out = sce->AAC_RENAME(output);
INTFLOAT *saved = sce->AAC_RENAME(saved);
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_long_960) : AAC_RENAME(sine_960);
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
INTFLOAT *buf = ac->AAC_RENAME(buf_mdct);
INTFLOAT *temp = ac->AAC_RENAME(temp);
int i;
// imdct
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
for (i = 0; i < 8; i++)
ac->mdct120_fn(ac->mdct120, buf + i * 120, in + i * 128, sizeof(INTFLOAT));
} else {
ac->mdct960_fn(ac->mdct960, buf, in, sizeof(INTFLOAT));
}
/* 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, 480);
} else {
memcpy( out, saved, 420 * sizeof(*out));
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
ac->fdsp->vector_fmul_window(out + 420 + 0*120, saved + 420, buf + 0*120, swindow_prev, 60);
ac->fdsp->vector_fmul_window(out + 420 + 1*120, buf + 0*120 + 60, buf + 1*120, swindow, 60);
ac->fdsp->vector_fmul_window(out + 420 + 2*120, buf + 1*120 + 60, buf + 2*120, swindow, 60);
ac->fdsp->vector_fmul_window(out + 420 + 3*120, buf + 2*120 + 60, buf + 3*120, swindow, 60);
ac->fdsp->vector_fmul_window(temp, buf + 3*120 + 60, buf + 4*120, swindow, 60);
memcpy( out + 420 + 4*120, temp, 60 * sizeof(*out));
} else {
ac->fdsp->vector_fmul_window(out + 420, saved + 420, buf, swindow_prev, 60);
memcpy( out + 540, buf + 60, 420 * sizeof(*out));
}
}
// buffer update
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
memcpy( saved, temp + 60, 60 * sizeof(*saved));
ac->fdsp->vector_fmul_window(saved + 60, buf + 4*120 + 60, buf + 5*120, swindow, 60);
ac->fdsp->vector_fmul_window(saved + 180, buf + 5*120 + 60, buf + 6*120, swindow, 60);
ac->fdsp->vector_fmul_window(saved + 300, buf + 6*120 + 60, buf + 7*120, swindow, 60);
memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
memcpy( saved, buf + 480, 420 * sizeof(*saved));
memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
} else { // LONG_STOP or ONLY_LONG
memcpy( saved, buf + 480, 480 * sizeof(*saved));
}
}
static void AAC_RENAME(imdct_and_windowing_ld)(AACDecContext *ac, SingleChannelElement *sce)
{
IndividualChannelStream *ics = &sce->ics;
INTFLOAT *in = sce->AAC_RENAME(coeffs);
INTFLOAT *out = sce->AAC_RENAME(output);
INTFLOAT *saved = sce->AAC_RENAME(saved);
INTFLOAT *buf = ac->AAC_RENAME(buf_mdct);
// imdct
ac->mdct512_fn(ac->mdct512, buf, in, sizeof(INTFLOAT));
// window overlapping
if (ics->use_kb_window[1]) {
// AAC LD uses a low overlap sine window instead of a KBD window
memcpy(out, saved, 192 * sizeof(*out));
ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME2(sine_128), 64);
memcpy( out + 320, buf + 64, 192 * sizeof(*out));
} else {
ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME2(sine_512), 256);
}
// buffer update
memcpy(saved, buf + 256, 256 * sizeof(*saved));
}
static void AAC_RENAME(imdct_and_windowing_eld)(AACDecContext *ac, SingleChannelElement *sce)
{
UINTFLOAT *in = sce->AAC_RENAME(coeffs);
INTFLOAT *out = sce->AAC_RENAME(output);
INTFLOAT *saved = sce->AAC_RENAME(saved);
INTFLOAT *buf = ac->AAC_RENAME(buf_mdct);
int i;
const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
const int n2 = n >> 1;
const int n4 = n >> 2;
const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
AAC_RENAME(ff_aac_eld_window_512);
// Inverse transform, mapped to the conventional IMDCT by
// Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
// "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
// International Conference on Audio, Language and Image Processing, ICALIP 2008.
// URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
for (i = 0; i < n2; i+=2) {
INTFLOAT temp;
temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
}
if (n == 480)
ac->mdct480_fn(ac->mdct480, buf, in, sizeof(INTFLOAT));
else
ac->mdct512_fn(ac->mdct512, buf, in, sizeof(INTFLOAT));
for (i = 0; i < n; i+=2) {
buf[i + 0] = -(UINTFLOAT)(USE_FIXED + 1)*buf[i + 0];
buf[i + 1] = (UINTFLOAT)(USE_FIXED + 1)*buf[i + 1];
}
// Like with the regular IMDCT at this point we still have the middle half
// of a transform but with even symmetry on the left and odd symmetry on
// the right
// window overlapping
// The spec says to use samples [0..511] but the reference decoder uses
// samples [128..639].
for (i = n4; i < n2; i ++) {
out[i - n4] = AAC_MUL31( buf[ n2 - 1 - i] , window[i - n4]) +
AAC_MUL31( saved[ i + n2] , window[i + n - n4]) +
AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
AAC_MUL31(-saved[ 2*n + n2 + i] , window[i + 3*n - n4]);
}
for (i = 0; i < n2; i ++) {
out[n4 + i] = AAC_MUL31( buf[ i] , window[i + n2 - n4]) +
AAC_MUL31(-saved[ n - 1 - i] , window[i + n2 + n - n4]) +
AAC_MUL31(-saved[ n + i] , window[i + n2 + 2*n - n4]) +
AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
}
for (i = 0; i < n4; i ++) {
out[n2 + n4 + i] = AAC_MUL31( buf[ i + n2] , window[i + n - n4]) +
AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
}
// buffer update
memmove(saved + n, saved, 2 * n * sizeof(*saved));
memcpy( saved, buf, n * sizeof(*saved));
}
static void AAC_RENAME(clip_output)(AACDecContext *ac, ChannelElement *che,
int type, int samples)
{
#if USE_FIXED
/* preparation for resampler */
for (int j = 0; j < samples; j++){
che->ch[0].output_fixed[j] = (int32_t)av_clip64((int64_t)che->ch[0].output_fixed[j]*128,
INT32_MIN, INT32_MAX-0x8000)+0x8000;
if (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))
che->ch[1].output_fixed[j] = (int32_t)av_clip64((int64_t)che->ch[1].output_fixed[j]*128,
INT32_MIN, INT32_MAX-0x8000)+0x8000;
}
#endif
}
static inline void reset_all_predictors(PredictorState *ps)
{
int i;
for (i = 0; i < MAX_PREDICTORS; i++)
reset_predict_state(&ps[i]);
}
static inline 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]);
}
/**
* Apply AAC-Main style frequency domain prediction.
*/
static void AAC_RENAME(apply_prediction)(AACDecContext *ac, SingleChannelElement *sce)
{
int sfb, k;
if (!sce->ics.predictor_initialized) {
reset_all_predictors(sce->AAC_RENAME(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->AAC_RENAME(predictor_state)[k],
&sce->AAC_RENAME(coeffs)[k],
sce->ics.predictor_present &&
sce->ics.prediction_used[sfb]);
}
}
if (sce->ics.predictor_reset_group)
reset_predictor_group(sce->AAC_RENAME(predictor_state),
sce->ics.predictor_reset_group);
} else
reset_all_predictors(sce->AAC_RENAME(predictor_state));
}
static av_cold void AAC_RENAME(aac_dsp_init)(AACDecDSP *aac_dsp)
{
#define SET(member) aac_dsp->member = AAC_RENAME(member)
SET(dequant_scalefactors);
SET(apply_mid_side_stereo);
SET(apply_intensity_stereo);
SET(apply_tns);
SET(apply_ltp);
SET(update_ltp);
SET(apply_prediction);
SET(imdct_and_windowing);
SET(imdct_and_windowing_768);
SET(imdct_and_windowing_960);
SET(imdct_and_windowing_ld);
SET(imdct_and_windowing_eld);
SET(apply_dependent_coupling);
SET(apply_independent_coupling);
SET(clip_output);
#undef SET
}
@@ -0,0 +1,105 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#define USE_FIXED 1
#include "libavutil/thread.h"
#include "libavcodec/aac_defines.h"
#include "libavcodec/avcodec.h"
#include "aacdec.h"
#include "libavcodec/aactab.h"
#include "libavcodec/sinewin_fixed_tablegen.h"
#include "libavcodec/kbdwin.h"
#include "libavcodec/cbrt_data.h"
#include "libavcodec/aacsbr.h"
DECLARE_ALIGNED(32, static int, aac_kbd_long_1024_fixed)[1024];
DECLARE_ALIGNED(32, static int, aac_kbd_short_128_fixed)[128];
DECLARE_ALIGNED(32, static int, aac_kbd_long_960_fixed)[960];
DECLARE_ALIGNED(32, static int, aac_kbd_short_120_fixed)[120];
DECLARE_ALIGNED(32, static int, aac_kbd_long_768_fixed)[768];
DECLARE_ALIGNED(32, static int, aac_kbd_short_96_fixed)[96];
static void init_tables_fixed_fn(void)
{
ff_cbrt_tableinit_fixed();
ff_kbd_window_init_fixed(aac_kbd_long_1024_fixed, 4.0, 1024);
ff_kbd_window_init_fixed(aac_kbd_short_128_fixed, 6.0, 128);
ff_kbd_window_init_fixed(aac_kbd_long_960_fixed, 4.0, 960);
ff_kbd_window_init_fixed(aac_kbd_short_120_fixed, 6.0, 120);
ff_aac_sbr_init_fixed();
init_sine_windows_fixed();
}
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)
};
/** Dequantization-related */
#include "aacdec_fixed_dequant.h"
#include "aacdec_fixed_coupling.h"
#include "aacdec_fixed_prediction.h"
#include "aacdec_dsp_template.c"
#include "aacdec_proc_template.c"
av_cold int ff_aac_decode_init_fixed(AVCodecContext *avctx)
{
static AVOnce init_fixed_once = AV_ONCE_INIT;
AACDecContext *ac = avctx->priv_data;
ac->is_fixed = 1;
avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
aac_dsp_init_fixed(&ac->dsp);
aac_proc_init_fixed(&ac->proc);
ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
if (!ac->fdsp)
return AVERROR(ENOMEM);
ff_thread_once(&init_fixed_once, init_tables_fixed_fn);
return ff_aac_decode_init(avctx);
}
@@ -0,0 +1,137 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_FIXED_COUPLING_H
#define AVCODEC_AAC_AACDEC_FIXED_COUPLING_H
#include "aacdec.h"
/**
* Apply dependent channel coupling (applied before IMDCT).
*
* @param index index into coupling gain array
*/
static void AAC_RENAME(apply_dependent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index)
{
IndividualChannelStream *ics = &cce->ch[0].ics;
const uint16_t *offsets = ics->swb_offset;
int *dest = target->coeffs_fixed;
const int *src = cce->ch[0].coeffs_fixed;
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;
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 < -31) {
// Nothing to do
} else 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++) {
tmp = (int)(((int64_t)src[group * 128 + k] * c + \
(int64_t)0x1000000000) >> 37);
dest[group * 128 + k] += (tmp + (int64_t)round) >> shift;
}
}
}
else {
for (group = 0; group < ics->group_len[g]; group++) {
for (k = offsets[i]; k < offsets[i + 1]; k++) {
tmp = (int)(((int64_t)src[group * 128 + k] * c + \
(int64_t)0x1000000000) >> 37);
dest[group * 128 + k] += tmp * (1U << 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 AAC_RENAME(apply_independent_coupling)(AACDecContext *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].output_fixed;
unsigned int *dest = target->output_fixed;
const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
c = cce_scale_fixed[gain & 7];
shift = (gain-1024) >> 3;
if (shift < -31) {
return;
} else if (shift < 0) {
shift = -shift;
round = 1 << (shift - 1);
for (i = 0; i < len; i++) {
tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
dest[i] += (tmp + round) >> shift;
}
}
else {
for (i = 0; i < len; i++) {
tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
dest[i] += tmp * (1U << shift);
}
}
}
#endif /* AVCODEC_AAC_AACDEC_FIXED_COUPLING_H */
@@ -0,0 +1,174 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_FIXED_DEQUANT_H
#define AVCODEC_AAC_AACDEC_FIXED_DEQUANT_H
#include "aacdec_tab.h"
static void inline vector_pow43(int *coefs, int len)
{
int i, coef;
for (i=0; i<len; i++) {
coef = coefs[i];
if (coef < 0)
coef = -(int)ff_cbrt_tab_fixed[(-coef) & 8191];
else
coef = (int)ff_cbrt_tab_fixed[ coef & 8191];
coefs[i] = coef;
}
}
/* 2^0, 2^0.25, 2^0.5, 2^0.75 */
static const int exp2tab[4] = {
Q31(1.0000000000/2), Q31(1.1892071150/2),
Q31(1.4142135624/2), Q31(1.6817928305/2)
};
static void inline subband_scale(int *dst, int *src, int scale,
int offset, int len, void *log_context)
{
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 > 31) {
for (i=0; i<len; i++) {
dst[i] = 0;
}
} else if (s > 0) {
round = 1 << (s-1);
for (i=0; i<len; i++) {
out = (int)(((int64_t)src[i] * c) >> 32);
dst[i] = ((int)(out+round) >> s) * ssign;
}
} else if (s > -32) {
s = s + 32;
round = 1U << (s-1);
for (i=0; i<len; i++) {
out = (int)((int64_t)((int64_t)src[i] * c + round) >> s);
dst[i] = out * (unsigned)ssign;
}
} else {
av_log(log_context, AV_LOG_ERROR, "Overflow in subband_scale()\n");
}
}
static void noise_scale(int *coefs, int scale, int band_energy, int len)
{
int s = -scale;
unsigned int round;
int i, out, c = exp2tab[s & 3];
int nlz = 0;
av_assert0(s >= 0);
while (band_energy > 0x7fff) {
band_energy >>= 1;
nlz++;
}
c /= band_energy;
s = 21 + nlz - (s >> 2);
if (s > 31) {
for (i=0; i<len; i++) {
coefs[i] = 0;
}
} else if (s >= 0) {
round = s ? 1 << (s-1) : 0;
for (i=0; i<len; i++) {
out = (int)(((int64_t)coefs[i] * c) >> 32);
coefs[i] = -((int)(out+round) >> s);
}
}
else {
s = s + 32;
if (s > 0) {
round = 1 << (s-1);
for (i=0; i<len; i++) {
out = (int)((int64_t)((int64_t)coefs[i] * c + round) >> s);
coefs[i] = -out;
}
} else {
for (i=0; i<len; i++)
coefs[i] = -(int64_t)coefs[i] * c * (1 << -s);
}
}
}
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 - (sign & 0xFFFFFFFE));
dst[1] = (idx >> 4 & 15) * (1 - ((sign & 1) * 2));
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) * 2));
sign <<= nz & 1;
nz >>= 1;
dst[1] = (idx >> 2 & 3) * (1 + (((int)sign >> 31) * 2));
sign <<= nz & 1;
nz >>= 1;
dst[2] = (idx >> 4 & 3) * (1 + (((int)sign >> 31) * 2));
sign <<= nz & 1;
nz >>= 1;
dst[3] = (idx >> 6 & 3) * (1 + (((int)sign >> 31) * 2));
return dst + 4;
}
#endif /* AVCODEC_AAC_AACDEC_FIXED_DEQUANT_H */
@@ -0,0 +1,151 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H
#define AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H
static av_always_inline SoftFloat flt16_round(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
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 SoftFloat flt16_even(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
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 SoftFloat flt16_trunc(SoftFloat pf)
{
SoftFloat pun;
int s;
pun.exp = pf.exp;
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 SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
SoftFloat e0, e1;
SoftFloat pv;
SoftFloat k1, k2;
SoftFloat r0 = ps->r0, r1 = ps->r1;
SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
SoftFloat var0 = ps->var0, var1 = ps->var1;
SoftFloat tmp;
if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
}
else {
k1.mant = 0;
k1.exp = 0;
}
if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
}
else {
k2.mant = 0;
k2.exp = 0;
}
tmp = av_mul_sf(k1, r0);
pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
if (output_enable) {
int shift = 28 - pv.exp;
if (shift < 31) {
if (shift > 0) {
*coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
} else
*coef += (unsigned)pv.mant << -shift;
}
}
e0 = av_int2sf(*coef, 2);
e1 = av_sub_sf(e0, tmp);
ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
tmp.exp--;
ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
tmp.exp--;
ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
ps->r0 = flt16_trunc(av_mul_sf(a, e0));
}
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0.mant = 0;
ps->r0.exp = 0;
ps->r1.mant = 0;
ps->r1.exp = 0;
ps->cor0.mant = 0;
ps->cor0.exp = 0;
ps->cor1.mant = 0;
ps->cor1.exp = 0;
ps->var0.mant = 0x20000000;
ps->var0.exp = 1;
ps->var1.mant = 0x20000000;
ps->var1.exp = 1;
}
#endif /* AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H */
@@ -0,0 +1,182 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#define USE_FIXED 0
#include "libavutil/thread.h"
#include "libavcodec/aac_defines.h"
#include "libavcodec/avcodec.h"
#include "aacdec.h"
#include "libavcodec/aactab.h"
#include "libavcodec/sinewin.h"
#include "libavcodec/kbdwin.h"
#include "libavcodec/cbrt_data.h"
#include "libavutil/mathematics.h"
#include "libavcodec/aacsbr.h"
DECLARE_ALIGNED(32, static float, sine_96)[96];
DECLARE_ALIGNED(32, static float, sine_120)[120];
DECLARE_ALIGNED(32, static float, sine_768)[768];
DECLARE_ALIGNED(32, static float, sine_960)[960];
DECLARE_ALIGNED(32, static float, aac_kbd_long_960)[960];
DECLARE_ALIGNED(32, static float, aac_kbd_short_120)[120];
DECLARE_ALIGNED(32, static float, aac_kbd_long_768)[768];
DECLARE_ALIGNED(32, static float, aac_kbd_short_96)[96];
static void init_tables_float_fn(void)
{
ff_cbrt_tableinit();
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_kbd_window_init(aac_kbd_long_960, 4.0, 960);
ff_kbd_window_init(aac_kbd_short_120, 6.0, 120);
ff_sine_window_init(sine_960, 960);
ff_sine_window_init(sine_120, 120);
ff_init_ff_sine_windows(9);
ff_aac_sbr_init();
ff_aac_float_common_init();
}
static const float cce_scale[] = {
1.09050773266525765921, //2^(1/8)
1.18920711500272106672, //2^(1/4)
M_SQRT2,
2,
};
/** Dequantization-related **/
#include "aacdec_tab.h"
#include "libavutil/intfloat.h"
#include "config.h"
#if ARCH_ARM
#include "libavcodec/arm/aac.h"
#endif
#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
#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
#include "aacdec_float_coupling.h"
#include "aacdec_float_prediction.h"
#include "aacdec_dsp_template.c"
#include "aacdec_proc_template.c"
av_cold int ff_aac_decode_init_float(AVCodecContext *avctx)
{
static AVOnce init_float_once = AV_ONCE_INIT;
AACDecContext *ac = avctx->priv_data;
ac->is_fixed = 0;
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
aac_dsp_init(&ac->dsp);
aac_proc_init(&ac->proc);
ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
if (!ac->fdsp)
return AVERROR(ENOMEM);
ff_thread_once(&init_float_once, init_tables_float_fn);
return ff_aac_decode_init(avctx);
}
@@ -0,0 +1,90 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_FLOAT_COUPLING_H
#define AVCODEC_AAC_AACDEC_FLOAT_COUPLING_H
#include "aacdec.h"
/**
* Apply dependent channel coupling (applied before IMDCT).
*
* @param index index into coupling gain array
*/
static void AAC_RENAME(apply_dependent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index)
{
IndividualChannelStream *ics = &cce->ch[0].ics;
const uint16_t *offsets = ics->swb_offset;
float *dest = target->coeffs;
const float *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 float gain = cce->coup.gain[index][idx];
for (group = 0; group < ics->group_len[g]; group++) {
for (k = offsets[i]; k < offsets[i + 1]; k++) {
// FIXME: SIMDify
dest[group * 128 + k] += gain * src[group * 128 + k];
}
}
}
}
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 AAC_RENAME(apply_independent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index)
{
const float gain = cce->coup.gain[index][0];
const float *src = cce->ch[0].output;
float *dest = target->output;
const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
ac->fdsp->vector_fmac_scalar(dest, src, gain, len);
}
#endif /* AVCODEC_AAC_AACDEC_FLOAT_COUPLING_H */
@@ -0,0 +1,100 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H
#define AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H
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);
}
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0 = 0.0f;
ps->r1 = 0.0f;
ps->cor0 = 0.0f;
ps->cor1 = 0.0f;
ps->var0 = 1.0f;
ps->var1 = 1.0f;
}
#endif /* AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H */
@@ -0,0 +1,352 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
#ifndef AVCODEC_AAC_AACDEC_LATM_H
#define AVCODEC_AAC_AACDEC_LATM_H
#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
struct LATMContext {
AACDecContext aac_ctx; ///< containing AACContext
int initialized; ///< initialized after a valid extradata was seen
// parser data
int audio_mux_version_A; ///< LATM syntax version
int frame_length_type; ///< 0/1 variable/fixed frame length
int frame_length; ///< frame length for fixed frame length
};
static inline uint32_t latm_get_value(GetBitContext *b)
{
int length = get_bits(b, 2);
return get_bits_long(b, (length+1)*8);
}
static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
GetBitContext *gb, int asclen)
{
AACDecContext *ac = &latmctx->aac_ctx;
AVCodecContext *avctx = ac->avctx;
OutputConfiguration oc = { 0 };
MPEG4AudioConfig *m4ac = &oc.m4ac;
GetBitContext gbc;
int config_start_bit = get_bits_count(gb);
int sync_extension = 0;
int bits_consumed, esize, i;
if (asclen > 0) {
sync_extension = 1;
asclen = FFMIN(asclen, get_bits_left(gb));
init_get_bits(&gbc, gb->buffer, config_start_bit + asclen);
skip_bits_long(&gbc, config_start_bit);
} else if (asclen == 0) {
gbc = *gb;
} else {
return AVERROR_INVALIDDATA;
}
if (get_bits_left(gb) <= 0)
return AVERROR_INVALIDDATA;
bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &oc,
&gbc, config_start_bit,
sync_extension);
if (bits_consumed < config_start_bit)
return AVERROR_INVALIDDATA;
bits_consumed -= config_start_bit;
if (asclen == 0)
asclen = bits_consumed;
if (!latmctx->initialized ||
ac->oc[1].m4ac.sample_rate != m4ac->sample_rate ||
ac->oc[1].m4ac.chan_config != m4ac->chan_config) {
if (latmctx->initialized) {
av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n",
m4ac->sample_rate, m4ac->chan_config);
} else {
av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
}
latmctx->initialized = 0;
esize = (asclen + 7) / 8;
if (avctx->extradata_size < esize) {
av_free(avctx->extradata);
avctx->extradata = av_malloc(esize + AV_INPUT_BUFFER_PADDING_SIZE);
if (!avctx->extradata)
return AVERROR(ENOMEM);
}
avctx->extradata_size = esize;
gbc = *gb;
for (i = 0; i < esize; i++) {
avctx->extradata[i] = get_bits(&gbc, 8);
}
memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
}
skip_bits_long(gb, asclen);
return 0;
}
static int read_stream_mux_config(struct LATMContext *latmctx,
GetBitContext *gb)
{
int ret, audio_mux_version = get_bits(gb, 1);
latmctx->audio_mux_version_A = 0;
if (audio_mux_version)
latmctx->audio_mux_version_A = get_bits(gb, 1);
if (!latmctx->audio_mux_version_A) {
if (audio_mux_version)
latm_get_value(gb); // taraFullness
skip_bits(gb, 1); // allStreamSameTimeFraming
skip_bits(gb, 6); // numSubFrames
// numPrograms
if (get_bits(gb, 4)) { // numPrograms
avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
return AVERROR_PATCHWELCOME;
}
// for each program (which there is only one in DVB)
// for each layer (which there is only one in DVB)
if (get_bits(gb, 3)) { // numLayer
avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
return AVERROR_PATCHWELCOME;
}
// for all but first stream: use_same_config = get_bits(gb, 1);
if (!audio_mux_version) {
if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
return ret;
} else {
int ascLen = latm_get_value(gb);
if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
return ret;
}
latmctx->frame_length_type = get_bits(gb, 3);
switch (latmctx->frame_length_type) {
case 0:
skip_bits(gb, 8); // latmBufferFullness
break;
case 1:
latmctx->frame_length = get_bits(gb, 9);
break;
case 3:
case 4:
case 5:
skip_bits(gb, 6); // CELP frame length table index
break;
case 6:
case 7:
skip_bits(gb, 1); // HVXC frame length table index
break;
}
if (get_bits(gb, 1)) { // other data
if (audio_mux_version) {
latm_get_value(gb); // other_data_bits
} else {
int esc;
do {
if (get_bits_left(gb) < 9)
return AVERROR_INVALIDDATA;
esc = get_bits(gb, 1);
skip_bits(gb, 8);
} while (esc);
}
}
if (get_bits(gb, 1)) // crc present
skip_bits(gb, 8); // config_crc
}
return 0;
}
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
{
uint8_t tmp;
if (ctx->frame_length_type == 0) {
int mux_slot_length = 0;
do {
if (get_bits_left(gb) < 8)
return AVERROR_INVALIDDATA;
tmp = get_bits(gb, 8);
mux_slot_length += tmp;
} while (tmp == 255);
return mux_slot_length;
} else if (ctx->frame_length_type == 1) {
return ctx->frame_length;
} else if (ctx->frame_length_type == 3 ||
ctx->frame_length_type == 5 ||
ctx->frame_length_type == 7) {
skip_bits(gb, 2); // mux_slot_length_coded
}
return 0;
}
static int read_audio_mux_element(struct LATMContext *latmctx,
GetBitContext *gb)
{
int err;
uint8_t use_same_mux = get_bits(gb, 1);
if (!use_same_mux) {
if ((err = read_stream_mux_config(latmctx, gb)) < 0)
return err;
} else if (!latmctx->aac_ctx.avctx->extradata) {
av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
"no decoder config found\n");
return 1;
}
if (latmctx->audio_mux_version_A == 0) {
int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
if (mux_slot_length_bytes < 0 || mux_slot_length_bytes * 8LL > get_bits_left(gb)) {
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
return AVERROR_INVALIDDATA;
} else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
"frame length mismatch %d << %d\n",
mux_slot_length_bytes * 8, get_bits_left(gb));
return AVERROR_INVALIDDATA;
}
}
return 0;
}
static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out,
int *got_frame_ptr, AVPacket *avpkt)
{
struct LATMContext *latmctx = avctx->priv_data;
int muxlength, err;
GetBitContext gb;
if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
return err;
// check for LOAS sync word
if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
return AVERROR_INVALIDDATA;
muxlength = get_bits(&gb, 13) + 3;
// not enough data, the parser should have sorted this out
if (muxlength > avpkt->size)
return AVERROR_INVALIDDATA;
if ((err = read_audio_mux_element(latmctx, &gb)))
return (err < 0) ? err : avpkt->size;
if (!latmctx->initialized) {
if (!avctx->extradata) {
*got_frame_ptr = 0;
return avpkt->size;
} else {
push_output_configuration(&latmctx->aac_ctx);
if ((err = decode_audio_specific_config(
&latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1],
avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
pop_output_configuration(&latmctx->aac_ctx);
return err;
}
latmctx->initialized = 1;
}
}
if (show_bits(&gb, 12) == 0xfff) {
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
"ADTS header detected, probably as result of configuration "
"misparsing\n");
return AVERROR_INVALIDDATA;
}
switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
case AOT_ER_AAC_LC:
case AOT_ER_AAC_LTP:
case AOT_ER_AAC_LD:
case AOT_ER_AAC_ELD:
err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
break;
default:
err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
}
if (err < 0)
return err;
return muxlength;
}
static av_cold int latm_decode_init(AVCodecContext *avctx)
{
struct LATMContext *latmctx = avctx->priv_data;
int ret = ff_aac_decode_init_float(avctx);
if (avctx->extradata_size > 0)
latmctx->initialized = !ret;
return ret;
}
/*
Note: This decoder filter is intended to decode LATM streams transferred
in MPEG transport streams which only contain one program.
To do a more complex LATM demuxing a separate LATM demuxer should be used.
*/
const FFCodec ff_aac_latm_decoder = {
.p.name = "aac_latm",
CODEC_LONG_NAME("AAC LATM (Advanced Audio Coding LATM syntax)"),
.p.type = AVMEDIA_TYPE_AUDIO,
.p.id = AV_CODEC_ID_AAC_LATM,
.priv_data_size = sizeof(struct LATMContext),
.init = latm_decode_init,
.close = decode_close,
FF_CODEC_DECODE_CB(latm_decode_frame),
.p.sample_fmts = (const enum AVSampleFormat[]) {
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
},
.p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
.p.ch_layouts = ff_aac_ch_layout,
.flush = flush,
.p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
};
#endif /* AVCODEC_AAC_AACDEC_LATM_H */
@@ -0,0 +1,201 @@
/*
* Copyright (c) 2024 Lynne <dev@lynne.ee>
*
* 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 "aacdec_lpd.h"
#include "aacdec_usac.h"
#include "libavcodec/unary.h"
const uint8_t ff_aac_lpd_mode_tab[32][4] = {
{ 0, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 1, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 1, 0, 1, 0 },
{ 0, 1, 1, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 0, 1 },
{ 1, 0, 0, 1 },
{ 0, 1, 0, 1 },
{ 1, 1, 0, 1 },
{ 0, 0, 1, 1 },
{ 1, 0, 1, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 2, 2, 0, 0 },
{ 2, 2, 1, 0 },
{ 2, 2, 0, 1 },
{ 2, 2, 1, 1 },
{ 0, 0, 2, 2 },
{ 1, 0, 2, 2 },
{ 0, 1, 2, 2 },
{ 1, 1, 2, 2 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
/* Larger values are reserved, but permit them for resilience */
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
};
static void parse_qn(GetBitContext *gb, int *qn, int nk_mode, int no_qn)
{
if (nk_mode == 1) {
for (int k = 0; k < no_qn; k++) {
qn[k] = get_unary(gb, 0, 68); // TODO: find proper ranges
if (qn[k])
qn[k]++;
}
return;
}
for (int k = 0; k < no_qn; k++)
qn[k] = get_bits(gb, 2) + 2;
if (nk_mode == 2) {
for (int k = 0; k < no_qn; k++) {
if (qn[k] > 4) {
qn[k] = get_unary(gb, 0, 65);
if (qn[k])
qn[k] += 4;
}
}
return;
}
for (int k = 0; k < no_qn; k++) {
if (qn[k] > 4) {
int qn_ext = get_unary(gb, 0, 65);
switch (qn_ext) {
case 0: qn[k] = 5; break;
case 1: qn[k] = 6; break;
case 2: qn[k] = 0; break;
default: qn[k] = qn_ext + 4; break;
}
}
}
}
static int parse_codebook_idx(GetBitContext *gb, uint32_t *kv,
int nk_mode, int no_qn)
{
int n, nk;
int qn[2];
parse_qn(gb, qn, nk_mode, no_qn);
for (int k = 0; k < no_qn; k++) {
if (qn[k] > 4) {
nk = (qn[k] - 3) / 2;
n = qn[k] - nk*2;
} else {
nk = 0;
n = qn[k];
}
}
if (nk > 25)
return AVERROR_PATCHWELCOME;
skip_bits(gb, 4*n);
if (nk > 0)
for (int i = 0; i < 8; i++)
kv[i] = get_bits(gb, nk);
return 0;
}
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb,
int use_gain, int len)
{
int ret;
if (use_gain)
ce->fac.gain = get_bits(gb, 7);
if (len/8 > 8)
return AVERROR_PATCHWELCOME;
for (int i = 0; i < len/8; i++) {
ret = parse_codebook_idx(gb, ce->fac.kv[i], 1, 1);
if (ret < 0)
return ret;
}
return 0;
}
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac,
AACUsacElemData *ce, GetBitContext *gb)
{
int k;
const uint8_t *mod;
int first_ldp_flag;
ce->ldp.acelp_core_mode = get_bits(gb, 3);
ce->ldp.lpd_mode = get_bits(gb, 5);
ce->ldp.bpf_control_info = get_bits1(gb);
ce->ldp.core_mode_last = get_bits1(gb);
ce->ldp.fac_data_present = get_bits1(gb);
mod = ff_aac_lpd_mode_tab[ce->ldp.lpd_mode];
first_ldp_flag = !ce->ldp.core_mode_last;
if (first_ldp_flag)
ce->ldp.last_lpd_mode = -1; /* last_ldp_mode is a **STATEFUL** value */
k = 0;
while (k < 0) {
if (!k) {
if (ce->ldp.core_mode_last && ce->ldp.fac_data_present)
ff_aac_parse_fac_data(ce, gb, 0, usac->core_frame_len/8);
} else {
if (!ce->ldp.last_lpd_mode && mod[k] > 0 ||
ce->ldp.last_lpd_mode && !mod[k])
ff_aac_parse_fac_data(ce, gb, 0, usac->core_frame_len/8);
}
if (!mod[k]) {
// parse_acelp_coding();
ce->ldp.last_lpd_mode = 0;
k++;
} else {
// parse_tcx_coding();
ce->ldp.last_lpd_mode = mod[k];
k += (1 << (mod[k] - 1));
}
}
// parse_lpc_data(first_lpd_flag);
if (!ce->ldp.core_mode_last && ce->ldp.fac_data_present) {
uint16_t len_8 = usac->core_frame_len / 8;
uint16_t len_16 = usac->core_frame_len / 16;
uint16_t fac_len = get_bits1(gb) /* short_fac_flag */ ? len_8 : len_16;
int ret = ff_aac_parse_fac_data(ce, gb, 1, fac_len);
if (ret < 0)
return ret;
}
return 0;
}
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Lynne <dev@lynne.ee>
*
* 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 AVCODEC_AAC_AACDEC_LPD_H
#define AVCODEC_AAC_AACDEC_LPD_H
#include "aacdec.h"
#include "libavcodec/get_bits.h"
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb,
int use_gain, int len);
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac,
AACUsacElemData *ce, GetBitContext *gb);
#endif /* AVCODEC_AAC_AACDEC_LPD_H */
@@ -0,0 +1,448 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@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
*/
/**
* 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;
}
/**
* 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 AAC_RENAME(decode_spectrum_and_dequant)(AACDecContext *ac,
GetBitContext *gb,
const Pulse *pulse,
SingleChannelElement *sce)
{
int i, k, g, idx = 0;
INTFLOAT *coef = sce->AAC_RENAME(coeffs);
IndividualChannelStream *ics = &sce->ics;
const int c = 1024 / ics->num_windows;
const uint16_t *offsets = ics->swb_offset;
const INTFLOAT *sf = sce->AAC_RENAME(sf);
const enum BandType *band_type = sce->band_type;
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(*cfo));
}
} else if (cbt_m1 == NOISE_BT - 1) {
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
INTFLOAT band_energy;
#if USE_FIXED
for (k = 0; k < off_len; k++) {
ac->random_state = lcg_random(ac->random_state);
cfo[k] = ac->random_state >> 3;
}
band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
band_energy = fixed_sqrt(band_energy, 31);
noise_scale(cfo, sf[idx], band_energy, off_len);
#else
float scale;
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);
#endif /* USE_FIXED */
}
} else {
#if !USE_FIXED
const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
#endif /* !USE_FIXED */
const VLCElem *vlc_tab = ff_vlc_spectral[cbt_m1];
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 = code;
#if USE_FIXED
cf = DEC_SQUAD(cf, cb_idx);
#else
cf = VMUL4(cf, vq, cb_idx, sf + idx);
#endif /* USE_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 = code;
nnz = cb_idx >> 8 & 15;
bits = nnz ? GET_CACHE(re, gb) : 0;
LAST_SKIP_BITS(re, gb, nnz);
#if USE_FIXED
cf = DEC_UQUAD(cf, cb_idx, bits);
#else
cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
#endif /* USE_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 = code;
#if USE_FIXED
cf = DEC_SPAIR(cf, cb_idx);
#else
cf = VMUL2(cf, vq, cb_idx, sf + idx);
#endif /* USE_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 = code;
nnz = cb_idx >> 8 & 15;
sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
LAST_SKIP_BITS(re, gb, nnz);
#if USE_FIXED
cf = DEC_UPAIR(cf, cb_idx, sign);
#else
cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
#endif /* USE_FIXED */
} while (len -= 2);
}
break;
default:
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
#if USE_FIXED
int *icf = cfo;
int v;
#else
float *cf = cfo;
uint32_t *icf = (uint32_t *) cf;
#endif /* USE_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);
cb_idx = code;
if (cb_idx == 0x0000) {
*icf++ = 0;
*icf++ = 0;
continue;
}
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 USE_FIXED
v = n;
if (bits & 1U<<31)
v = -v;
*icf++ = v;
#else
*icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
#endif /* USE_FIXED */
bits <<= 1;
} else {
#if USE_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 /* USE_FIXED */
bits <<= !!v;
}
cb_idx >>= 4;
}
} while (len -= 2);
#if !USE_FIXED
ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
#endif /* !USE_FIXED */
}
}
CLOSE_READER(re, gb);
}
}
coef += g_len << 7;
}
if (pulse) {
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 USE_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 /* USE_FIXED */
}
}
}
#if USE_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) {
vector_pow43(cfo, off_len);
subband_scale(cfo, cfo, sf[idx], 34, off_len, ac->avctx);
}
}
}
coef += g_len << 7;
}
#endif /* USE_FIXED */
return 0;
}
/**
* Decode coupling_channel_element; reference: table 4.8.
*
* @return Returns error status. 0 - OK, !0 - error
*/
static int AAC_RENAME(decode_cce)(AACDecContext *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);
#if USE_FIXED
scale = get_bits(gb, 2);
#else
scale = cce_scale[get_bits(gb, 2)];
#endif
if ((ret = ff_aac_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, ff_vlc_scalefactors, 7, 3) - 60: 0;
gain_cache = GET_GAIN(scale, gain);
#if USE_FIXED
if ((abs(gain_cache)-1024) >> 3 > 30)
return AVERROR(ERANGE);
#endif
}
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, ff_vlc_scalefactors, 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;
#if USE_FIXED
if ((abs(gain_cache)-1024) >> 3 > 30)
return AVERROR(ERANGE);
#endif
}
}
coup->gain[c][idx] = gain_cache;
}
}
}
}
}
return 0;
}
static av_cold void AAC_RENAME(aac_proc_init)(AACDecProc *aac_proc)
{
#define SET(member) aac_proc->member = AAC_RENAME(member)
SET(decode_spectrum_and_dequant);
SET(decode_cce);
#undef SET
#define SET(member) aac_proc->member = AV_JOIN(ff_aac_, AAC_RENAME(member));
SET(sbr_ctx_alloc_init);
SET(sbr_decode_extension);
SET(sbr_apply);
SET(sbr_ctx_close);
#undef SET
}
@@ -0,0 +1,308 @@
/*
* Common code and tables of the AAC fixed- and floating-point decoders
* 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
* Common code and tables of the AAC fixed- and floating-point decoders
*/
#include "aacdec_tab.h"
#include "libavcodec/aac.h"
#include "libavcodec/aacps.h"
#include "libavcodec/aactab.h"
#include "libavcodec/vlc.h"
#include "libavutil/attributes.h"
#include "libavutil/thread.h"
const int8_t ff_tags_per_config[16] = { 0, 1, 1, 2, 3, 3, 4, 5, 0, 0, 0, 5, 5, 16, 5, 0 };
const uint8_t ff_aac_channel_layout_map[16][16][3] = {
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, },
{ { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_SCE, 1, AAC_CHANNEL_BACK }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_BACK }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_BACK }, { TYPE_LFE, 0, AAC_CHANNEL_LFE }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_FRONT }, { TYPE_CPE, 2, AAC_CHANNEL_BACK }, { TYPE_LFE, 0, AAC_CHANNEL_LFE }, },
{ { 0, } },
{ { 0, } },
{ { 0, } },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_BACK }, { TYPE_SCE, 1, AAC_CHANNEL_BACK }, { TYPE_LFE, 0, AAC_CHANNEL_LFE }, },
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_BACK }, { TYPE_CPE, 2, AAC_CHANNEL_BACK }, { TYPE_LFE, 0, AAC_CHANNEL_LFE }, },
{
{ TYPE_SCE, 0, AAC_CHANNEL_FRONT }, // SCE1 = FC,
{ TYPE_CPE, 0, AAC_CHANNEL_FRONT }, // CPE1 = FLc and FRc,
{ TYPE_CPE, 1, AAC_CHANNEL_FRONT }, // CPE2 = FL and FR,
{ TYPE_CPE, 2, AAC_CHANNEL_BACK }, // CPE3 = SiL and SiR,
{ TYPE_CPE, 3, AAC_CHANNEL_BACK }, // CPE4 = BL and BR,
{ TYPE_SCE, 1, AAC_CHANNEL_BACK }, // SCE2 = BC,
{ TYPE_LFE, 0, AAC_CHANNEL_LFE }, // LFE1 = LFE1,
{ TYPE_LFE, 1, AAC_CHANNEL_LFE }, // LFE2 = LFE2,
{ TYPE_SCE, 2, AAC_CHANNEL_FRONT }, // SCE3 = TpFC,
{ TYPE_CPE, 4, AAC_CHANNEL_FRONT }, // CPE5 = TpFL and TpFR,
{ TYPE_CPE, 5, AAC_CHANNEL_SIDE }, // CPE6 = TpSiL and TpSiR,
{ TYPE_SCE, 3, AAC_CHANNEL_SIDE }, // SCE4 = TpC,
{ TYPE_CPE, 6, AAC_CHANNEL_BACK }, // CPE7 = TpBL and TpBR,
{ TYPE_SCE, 4, AAC_CHANNEL_BACK }, // SCE5 = TpBC,
{ TYPE_SCE, 5, AAC_CHANNEL_FRONT }, // SCE6 = BtFC,
{ TYPE_CPE, 7, AAC_CHANNEL_FRONT }, // CPE8 = BtFL and BtFR
},
{ { TYPE_SCE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 0, AAC_CHANNEL_FRONT }, { TYPE_CPE, 1, AAC_CHANNEL_BACK }, { TYPE_LFE, 0, AAC_CHANNEL_LFE }, { TYPE_CPE, 2, AAC_CHANNEL_FRONT }, },
{ { 0, } },
};
const int16_t ff_aac_channel_map[3][4][6] = {
{
{ AV_CHAN_FRONT_CENTER, AV_CHAN_FRONT_LEFT_OF_CENTER, AV_CHAN_FRONT_RIGHT_OF_CENTER, AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_NONE },
{ AV_CHAN_UNUSED, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE },
{ AV_CHAN_UNUSED, AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT, AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT, AV_CHAN_BACK_CENTER },
{ AV_CHAN_LOW_FREQUENCY, AV_CHAN_LOW_FREQUENCY_2, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE },
},
{
{ AV_CHAN_TOP_FRONT_CENTER, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_TOP_FRONT_LEFT, AV_CHAN_TOP_FRONT_RIGHT, AV_CHAN_NONE },
{ AV_CHAN_UNUSED, AV_CHAN_TOP_SIDE_LEFT, AV_CHAN_TOP_SIDE_RIGHT, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_TOP_CENTER},
{ AV_CHAN_UNUSED, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_TOP_BACK_LEFT, AV_CHAN_TOP_BACK_RIGHT, AV_CHAN_TOP_BACK_CENTER},
{ AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE},
},
{
{ AV_CHAN_BOTTOM_FRONT_CENTER, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_BOTTOM_FRONT_LEFT, AV_CHAN_BOTTOM_FRONT_RIGHT, AV_CHAN_NONE },
{ AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE },
{ AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE },
{ AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE, AV_CHAN_NONE },
},
};
const AVChannelLayout ff_aac_ch_layout[] = {
AV_CHANNEL_LAYOUT_MONO,
AV_CHANNEL_LAYOUT_STEREO,
AV_CHANNEL_LAYOUT_SURROUND,
AV_CHANNEL_LAYOUT_4POINT0,
AV_CHANNEL_LAYOUT_5POINT0_BACK,
AV_CHANNEL_LAYOUT_5POINT1_BACK,
AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
AV_CHANNEL_LAYOUT_6POINT1_BACK,
AV_CHANNEL_LAYOUT_7POINT1,
AV_CHANNEL_LAYOUT_22POINT2,
AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK,
{ 0 },
};
VLCElem ff_vlc_scalefactors[352];
const VLCElem *ff_vlc_spectral[11];
/// Huffman tables for SBR
static const uint8_t sbr_huffman_tab[][2] = {
/* t_huffman_env_1_5dB - 121 entries */
{ 60, 2 }, { 59, 2 }, { 61, 3 }, { 58, 3 }, { 62, 4 },
{ 57, 4 }, { 63, 5 }, { 56, 5 }, { 64, 6 }, { 55, 6 },
{ 65, 7 }, { 54, 7 }, { 66, 8 }, { 53, 8 }, { 67, 9 },
{ 52, 9 }, { 51, 10 }, { 68, 10 }, { 50, 11 }, { 69, 12 },
{ 49, 12 }, { 70, 13 }, { 48, 13 }, { 47, 13 }, { 71, 14 },
{ 46, 14 }, { 72, 14 }, { 45, 14 }, { 44, 15 }, { 73, 15 },
{ 41, 16 }, { 42, 16 }, { 43, 16 }, { 74, 16 }, { 36, 16 },
{ 40, 16 }, { 76, 16 }, { 34, 17 }, { 39, 17 }, { 75, 17 },
{ 37, 17 }, { 35, 18 }, { 38, 18 }, { 0, 18 }, { 1, 18 },
{ 2, 18 }, { 3, 18 }, { 4, 18 }, { 5, 18 }, { 6, 19 },
{ 7, 19 }, { 8, 19 }, { 9, 19 }, { 10, 19 }, { 11, 19 },
{ 12, 19 }, { 13, 19 }, { 14, 19 }, { 15, 19 }, { 16, 19 },
{ 17, 19 }, { 18, 19 }, { 19, 19 }, { 20, 19 }, { 21, 19 },
{ 22, 19 }, { 23, 19 }, { 24, 19 }, { 25, 19 }, { 26, 19 },
{ 27, 19 }, { 28, 19 }, { 29, 19 }, { 30, 19 }, { 31, 19 },
{ 32, 19 }, { 33, 19 }, { 77, 19 }, { 78, 19 }, { 79, 19 },
{ 80, 19 }, { 81, 19 }, { 82, 19 }, { 83, 19 }, { 84, 19 },
{ 85, 19 }, { 86, 19 }, { 87, 19 }, { 88, 19 }, { 89, 19 },
{ 90, 19 }, { 91, 19 }, { 92, 19 }, { 93, 19 }, { 94, 19 },
{ 95, 19 }, { 96, 19 }, { 97, 19 }, { 98, 19 }, { 99, 19 },
{ 100, 19 }, { 101, 19 }, { 102, 19 }, { 103, 19 }, { 104, 19 },
{ 105, 19 }, { 106, 19 }, { 107, 19 }, { 108, 19 }, { 109, 19 },
{ 110, 19 }, { 111, 19 }, { 112, 19 }, { 113, 19 }, { 114, 19 },
{ 115, 19 }, { 116, 19 }, { 117, 19 }, { 118, 19 }, { 119, 19 },
{ 120, 19 },
/* f_huffman_env_1_5dB - 121 entries */
{ 60, 2 }, { 59, 2 }, { 61, 3 }, { 58, 3 }, { 57, 4 },
{ 62, 4 }, { 56, 5 }, { 63, 5 }, { 55, 6 }, { 64, 6 },
{ 54, 7 }, { 65, 8 }, { 53, 8 }, { 66, 8 }, { 52, 9 },
{ 67, 9 }, { 51, 9 }, { 68, 10 }, { 50, 10 }, { 69, 11 },
{ 49, 11 }, { 70, 11 }, { 71, 11 }, { 48, 12 }, { 72, 12 },
{ 47, 12 }, { 73, 12 }, { 74, 13 }, { 46, 13 }, { 45, 13 },
{ 75, 13 }, { 76, 14 }, { 77, 14 }, { 44, 14 }, { 43, 15 },
{ 42, 15 }, { 41, 16 }, { 78, 16 }, { 79, 16 }, { 40, 16 },
{ 39, 16 }, { 80, 17 }, { 81, 17 }, { 36, 17 }, { 37, 17 },
{ 38, 17 }, { 34, 17 }, { 32, 18 }, { 82, 18 }, { 83, 18 },
{ 85, 18 }, { 19, 18 }, { 35, 18 }, { 86, 18 }, { 87, 18 },
{ 30, 18 }, { 33, 18 }, { 84, 18 }, { 88, 18 }, { 104, 18 },
{ 9, 19 }, { 14, 19 }, { 16, 19 }, { 17, 19 }, { 23, 19 },
{ 27, 19 }, { 29, 19 }, { 31, 19 }, { 90, 19 }, { 97, 19 },
{ 102, 19 }, { 107, 19 }, { 108, 19 }, { 0, 19 }, { 1, 19 },
{ 2, 20 }, { 3, 20 }, { 4, 20 }, { 5, 20 }, { 6, 20 },
{ 7, 20 }, { 8, 20 }, { 10, 20 }, { 11, 20 }, { 12, 20 },
{ 13, 20 }, { 15, 20 }, { 18, 20 }, { 20, 20 }, { 21, 20 },
{ 22, 20 }, { 24, 20 }, { 25, 20 }, { 26, 20 }, { 28, 20 },
{ 89, 20 }, { 91, 20 }, { 92, 20 }, { 93, 20 }, { 94, 20 },
{ 95, 20 }, { 96, 20 }, { 98, 20 }, { 99, 20 }, { 100, 20 },
{ 101, 20 }, { 103, 20 }, { 105, 20 }, { 106, 20 }, { 109, 20 },
{ 110, 20 }, { 111, 20 }, { 112, 20 }, { 113, 20 }, { 114, 20 },
{ 115, 20 }, { 116, 20 }, { 117, 20 }, { 118, 20 }, { 119, 20 },
{ 120, 20 },
/* t_huffman_env_bal_1_5dB - 49 entries */
{ 24, 1 }, { 25, 2 }, { 23, 3 }, { 26, 4 }, { 22, 5 },
{ 27, 6 }, { 21, 7 }, { 28, 8 }, { 20, 9 }, { 19, 11 },
{ 29, 11 }, { 18, 12 }, { 30, 12 }, { 31, 15 }, { 17, 16 },
{ 32, 16 }, { 0, 16 }, { 1, 16 }, { 2, 16 }, { 3, 16 },
{ 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 }, { 8, 16 },
{ 9, 16 }, { 10, 16 }, { 11, 16 }, { 12, 16 }, { 13, 16 },
{ 14, 16 }, { 15, 16 }, { 16, 16 }, { 33, 16 }, { 34, 16 },
{ 35, 16 }, { 36, 16 }, { 37, 16 }, { 38, 16 }, { 39, 17 },
{ 40, 17 }, { 41, 17 }, { 42, 17 }, { 43, 17 }, { 44, 17 },
{ 45, 17 }, { 46, 17 }, { 47, 17 }, { 48, 17 },
/* f_huffman_env_bal_1_5dB - 49 entries */
{ 24, 1 }, { 23, 2 }, { 25, 3 }, { 22, 4 }, { 26, 5 },
{ 27, 6 }, { 21, 7 }, { 20, 8 }, { 28, 9 }, { 19, 11 },
{ 29, 11 }, { 18, 11 }, { 30, 12 }, { 17, 14 }, { 31, 15 },
{ 32, 16 }, { 15, 16 }, { 16, 17 }, { 0, 18 }, { 1, 18 },
{ 2, 18 }, { 3, 18 }, { 4, 18 }, { 5, 18 }, { 6, 18 },
{ 7, 18 }, { 8, 18 }, { 9, 18 }, { 10, 18 }, { 11, 18 },
{ 12, 18 }, { 13, 18 }, { 14, 18 }, { 33, 18 }, { 34, 18 },
{ 35, 18 }, { 36, 18 }, { 37, 18 }, { 38, 18 }, { 39, 18 },
{ 40, 18 }, { 41, 18 }, { 42, 18 }, { 43, 18 }, { 44, 18 },
{ 45, 18 }, { 46, 18 }, { 47, 19 }, { 48, 19 },
/* t_huffman_env_3_0dB - 63 entries */
{ 31, 1 }, { 30, 2 }, { 32, 3 }, { 29, 4 }, { 33, 5 },
{ 28, 6 }, { 34, 7 }, { 27, 8 }, { 35, 9 }, { 26, 11 },
{ 36, 11 }, { 25, 12 }, { 24, 13 }, { 37, 13 }, { 23, 14 },
{ 38, 14 }, { 22, 14 }, { 21, 14 }, { 39, 14 }, { 40, 15 },
{ 41, 16 }, { 18, 16 }, { 20, 16 }, { 19, 16 }, { 17, 17 },
{ 42, 17 }, { 43, 18 }, { 0, 18 }, { 1, 18 }, { 2, 19 },
{ 3, 19 }, { 4, 19 }, { 5, 19 }, { 6, 19 }, { 7, 19 },
{ 8, 19 }, { 9, 19 }, { 10, 19 }, { 11, 19 }, { 12, 19 },
{ 13, 19 }, { 14, 19 }, { 15, 19 }, { 16, 19 }, { 44, 19 },
{ 45, 19 }, { 46, 19 }, { 47, 19 }, { 48, 19 }, { 49, 19 },
{ 50, 19 }, { 51, 19 }, { 52, 19 }, { 53, 19 }, { 54, 19 },
{ 55, 19 }, { 56, 19 }, { 57, 19 }, { 58, 19 }, { 59, 19 },
{ 60, 19 }, { 61, 19 }, { 62, 19 },
/* f_huffman_env_3_0dB - 63 entries */
{ 31, 1 }, { 30, 2 }, { 32, 3 }, { 29, 4 }, { 33, 5 },
{ 28, 6 }, { 34, 8 }, { 27, 8 }, { 35, 9 }, { 26, 9 },
{ 36, 10 }, { 25, 10 }, { 37, 11 }, { 24, 11 }, { 38, 12 },
{ 23, 12 }, { 39, 13 }, { 40, 14 }, { 22, 14 }, { 21, 15 },
{ 41, 15 }, { 42, 15 }, { 20, 16 }, { 19, 16 }, { 43, 16 },
{ 44, 16 }, { 18, 17 }, { 16, 17 }, { 45, 17 }, { 46, 17 },
{ 17, 18 }, { 49, 18 }, { 13, 18 }, { 7, 18 }, { 12, 18 },
{ 47, 18 }, { 48, 18 }, { 9, 19 }, { 10, 19 }, { 15, 19 },
{ 51, 19 }, { 52, 19 }, { 53, 19 }, { 56, 19 }, { 8, 19 },
{ 11, 19 }, { 55, 19 }, { 0, 20 }, { 1, 20 }, { 2, 20 },
{ 3, 20 }, { 4, 20 }, { 5, 20 }, { 6, 20 }, { 14, 20 },
{ 50, 20 }, { 54, 20 }, { 57, 20 }, { 58, 20 }, { 59, 20 },
{ 60, 20 }, { 61, 20 }, { 62, 20 },
/* t_huffman_env_bal_3_0dB - 25 entries */
{ 12, 1 }, { 13, 2 }, { 11, 3 }, { 10, 4 }, { 14, 5 },
{ 15, 6 }, { 9, 7 }, { 8, 8 }, { 16, 9 }, { 7, 12 },
{ 0, 13 }, { 1, 13 }, { 2, 13 }, { 3, 13 }, { 4, 13 },
{ 5, 13 }, { 6, 13 }, { 17, 13 }, { 18, 13 }, { 19, 13 },
{ 20, 13 }, { 21, 13 }, { 22, 13 }, { 23, 14 }, { 24, 14 },
/* f_huffman_env_bal_3_0dB - 25 entries */
{ 12, 1 }, { 11, 2 }, { 13, 3 }, { 10, 4 }, { 14, 5 },
{ 15, 6 }, { 9, 7 }, { 8, 8 }, { 16, 9 }, { 7, 11 },
{ 17, 12 }, { 18, 13 }, { 0, 13 }, { 1, 13 }, { 2, 13 },
{ 3, 13 }, { 4, 13 }, { 5, 14 }, { 6, 14 }, { 19, 14 },
{ 20, 14 }, { 21, 14 }, { 22, 14 }, { 23, 14 }, { 24, 14 },
/* t_huffman_noise_3_0dB - 63 entries */
{ 31, 1 }, { 32, 2 }, { 30, 3 }, { 29, 4 }, { 33, 5 },
{ 28, 6 }, { 34, 8 }, { 27, 8 }, { 35, 10 }, { 26, 11 },
{ 36, 13 }, { 42, 13 }, { 0, 13 }, { 1, 13 }, { 2, 13 },
{ 3, 13 }, { 4, 13 }, { 5, 13 }, { 6, 13 }, { 7, 13 },
{ 8, 13 }, { 9, 13 }, { 10, 13 }, { 11, 13 }, { 12, 13 },
{ 13, 13 }, { 14, 13 }, { 15, 13 }, { 16, 13 }, { 17, 13 },
{ 18, 13 }, { 19, 13 }, { 20, 13 }, { 21, 13 }, { 22, 13 },
{ 23, 13 }, { 24, 13 }, { 25, 13 }, { 37, 13 }, { 38, 13 },
{ 39, 13 }, { 40, 13 }, { 41, 13 }, { 43, 13 }, { 44, 13 },
{ 45, 13 }, { 46, 13 }, { 47, 13 }, { 48, 13 }, { 49, 13 },
{ 50, 13 }, { 51, 13 }, { 52, 13 }, { 53, 13 }, { 54, 13 },
{ 55, 13 }, { 56, 13 }, { 57, 13 }, { 58, 13 }, { 59, 13 },
{ 60, 13 }, { 61, 14 }, { 62, 14 },
/* t_huffman_noise_bal_3_0dB - 25 entries */
{ 12, 1 }, { 11, 2 }, { 13, 3 }, { 10, 5 }, { 14, 6 },
{ 0, 8 }, { 1, 8 }, { 2, 8 }, { 3, 8 }, { 4, 8 },
{ 5, 8 }, { 6, 8 }, { 7, 8 }, { 8, 8 }, { 9, 8 },
{ 15, 8 }, { 16, 8 }, { 17, 8 }, { 18, 8 }, { 19, 8 },
{ 20, 8 }, { 21, 8 }, { 22, 8 }, { 23, 8 }, { 24, 8 },
};
static const uint8_t sbr_huffman_nb_codes[] = {
121, 121, 49, 49, 63, 63, 25, 25, 63, 25
};
static const int8_t sbr_vlc_offsets[10] = {
-60, -60, -24, -24, -31, -31, -12, -12, -31, -12
};
const VLCElem *ff_aac_sbr_vlc[10];
static av_cold void aacdec_common_init(void)
{
static VLCElem vlc_buf[(304 + 270 + 550 + 300 + 328 +
294 + 306 + 268 + 510 + 366 + 462) +
(1098 + 1092 + 768 + 1026 + 1058 +
1052 + 544 + 544 + 592 + 512)];
VLCInitState state = VLC_INIT_STATE(vlc_buf);
const uint8_t (*tab)[2] = sbr_huffman_tab;
for (unsigned i = 0; i < 11; i++) {
#define TAB_WRAP_SIZE(name) name[i], sizeof(name[i][0]), sizeof(name[i][0])
ff_vlc_spectral[i] =
ff_vlc_init_tables_sparse(&state, 8, ff_aac_spectral_sizes[i],
TAB_WRAP_SIZE(ff_aac_spectral_bits),
TAB_WRAP_SIZE(ff_aac_spectral_codes),
TAB_WRAP_SIZE(ff_aac_codebook_vector_idx),
0);
}
VLC_INIT_STATIC_TABLE(ff_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]), 0);
// SBR VLC table initialization
for (int i = 0; i < FF_ARRAY_ELEMS(ff_aac_sbr_vlc); i++) {
ff_aac_sbr_vlc[i] =
ff_vlc_init_tables_from_lengths(&state, 9, sbr_huffman_nb_codes[i],
&tab[0][1], 2,
&tab[0][0], 2, 1,
sbr_vlc_offsets[i], 0);
tab += sbr_huffman_nb_codes[i];
}
ff_ps_init_common();
}
av_cold void ff_aacdec_common_init_once(void)
{
static AVOnce init_static_once = AV_ONCE_INIT;
ff_thread_once(&init_static_once, aacdec_common_init);
}
@@ -0,0 +1,55 @@
/*
* AAC decoder data
*
* 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 data
* @author Oded Shimon ( ods15 ods15 dyndns org )
* @author Maxim Gavrilov ( maxim.gavrilov gmail com )
*/
#ifndef AVCODEC_AAC_AACDEC_TAB_H
#define AVCODEC_AAC_AACDEC_TAB_H
#include <stdint.h>
#include "libavcodec/vlc.h"
#include "libavutil/attributes_internal.h"
#include "libavutil/channel_layout.h"
FF_VISIBILITY_PUSH_HIDDEN
void ff_aacdec_common_init_once(void);
extern const VLCElem *ff_aac_sbr_vlc[10];
extern VLCElem ff_vlc_scalefactors[];
extern const VLCElem *ff_vlc_spectral[11];
extern const int8_t ff_tags_per_config[16];
extern const uint8_t ff_aac_channel_layout_map[16][16][3];
extern const int16_t ff_aac_channel_map[3][4][6];
extern const AVChannelLayout ff_aac_ch_layout[];
FF_VISIBILITY_POP_HIDDEN
#endif /* AVCODEC_AAC_AACDEC_TAB_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Lynne <dev@lynne.ee>
*
* 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 AVCODEC_AAC_AACDEC_USAC_H
#define AVCODEC_AAC_AACDEC_USAC_H
#include "aacdec.h"
#include "libavcodec/get_bits.h"
int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
GetBitContext *gb, OutputConfiguration *oc,
int channel_config);
int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc);
int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
GetBitContext *gb, int *got_frame_ptr);
#endif /* AVCODEC_AAC_AACDEC_USAC_H */