* [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715
@ 2024-06-16 8:54 Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 2/6] aac: expose ff_aac_sample_rate_idx() in aac.h Lynne via ffmpeg-devel
` (4 more replies)
0 siblings, 5 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
---
libavcodec/aac/aacdec_usac.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
index 98e8c1c0bc..065bc869d9 100644
--- a/libavcodec/aac/aacdec_usac.c
+++ b/libavcodec/aac/aacdec_usac.c
@@ -835,6 +835,11 @@ static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac,
tns_active = get_bits1(gb);
us->common_window = get_bits1(gb);
+ if (!us->common_window || indep_flag) {
+ memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
+ memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
+ }
+
if (us->common_window) {
/* ics_info() */
ics1->window_sequence[1] = ics1->window_sequence[0];
@@ -845,6 +850,20 @@ static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac,
ics2->use_kb_window[1] = ics2->use_kb_window[0];
ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb);
+ /* If there's a change in the transform sequence, zero out last frame's
+ * stereo prediction coefficients */
+ if ((ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
+ ics1->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
+ (ics1->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
+ ics1->window_sequence[0] != EIGHT_SHORT_SEQUENCE) ||
+ (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
+ ics2->window_sequence[1] != EIGHT_SHORT_SEQUENCE) ||
+ (ics2->window_sequence[1] == EIGHT_SHORT_SEQUENCE &&
+ ics2->window_sequence[0] != EIGHT_SHORT_SEQUENCE)) {
+ memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
+ memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
+ }
+
if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4);
ue1->scale_factor_grouping = ue2->scale_factor_grouping = get_bits(gb, 7);
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* [FFmpeg-devel] [PATCH 2/6] aac: expose ff_aac_sample_rate_idx() in aac.h
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
@ 2024-06-16 8:54 ` Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 3/6] aacdec_ac: fix an overread Lynne via ffmpeg-devel
` (3 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
The rate index is a value important to both encoders and decoders.
USAC needs it as well, so put it into the shared main header.
---
libavcodec/aac.h | 16 ++++++++++++++++
libavcodec/aac/aacdec.c | 18 +-----------------
2 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/libavcodec/aac.h b/libavcodec/aac.h
index fc6d1361b2..78026a5887 100644
--- a/libavcodec/aac.h
+++ b/libavcodec/aac.h
@@ -103,4 +103,20 @@ typedef struct Pulse {
int amp[4];
} Pulse;
+static inline int ff_aac_sample_rate_idx(int rate)
+{
+ if (92017 <= rate) return 0;
+ else if (75132 <= rate) return 1;
+ else if (55426 <= rate) return 2;
+ else if (46009 <= rate) return 3;
+ else if (37566 <= rate) return 4;
+ else if (27713 <= rate) return 5;
+ else if (23004 <= rate) return 6;
+ else if (18783 <= rate) return 7;
+ else if (13856 <= rate) return 8;
+ else if (11502 <= rate) return 9;
+ else if (9391 <= rate) return 10;
+ else return 11;
+}
+
#endif /* AVCODEC_AAC_H */
diff --git a/libavcodec/aac/aacdec.c b/libavcodec/aac/aacdec.c
index eecb6d8f3d..ea2ba84a80 100644
--- a/libavcodec/aac/aacdec.c
+++ b/libavcodec/aac/aacdec.c
@@ -1095,22 +1095,6 @@ static int decode_audio_specific_config(AACDecContext *ac,
sync_extension);
}
-static int sample_rate_idx (int rate)
-{
- if (92017 <= rate) return 0;
- else if (75132 <= rate) return 1;
- else if (55426 <= rate) return 2;
- else if (46009 <= rate) return 3;
- else if (37566 <= rate) return 4;
- else if (27713 <= rate) return 5;
- else if (23004 <= rate) return 6;
- else if (18783 <= rate) return 7;
- else if (13856 <= rate) return 8;
- else if (11502 <= rate) return 9;
- else if (9391 <= rate) return 10;
- else return 11;
-}
-
static av_cold int decode_close(AVCodecContext *avctx)
{
AACDecContext *ac = avctx->priv_data;
@@ -1211,7 +1195,7 @@ av_cold int ff_aac_decode_init(AVCodecContext *avctx)
uint8_t layout_map[MAX_ELEM_ID*4][3];
int layout_map_tags;
- sr = sample_rate_idx(avctx->sample_rate);
+ sr = ff_aac_sample_rate_idx(avctx->sample_rate);
ac->oc[1].m4ac.sampling_index = sr;
ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
ac->oc[1].m4ac.sbr = -1;
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* [FFmpeg-devel] [PATCH 3/6] aacdec_ac: fix an overread
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 2/6] aac: expose ff_aac_sample_rate_idx() in aac.h Lynne via ffmpeg-devel
@ 2024-06-16 8:54 ` Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 4/6] aacdec_usac: rename noise_scale to noise_bands Lynne via ffmpeg-devel
` (2 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
Fixes reading state->last[i + 1] in ff_aac_ac_get_context for the
last array member.
---
libavcodec/aac/aacdec_ac.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/aac/aacdec_ac.h b/libavcodec/aac/aacdec_ac.h
index 0b98c0f0d9..b8d4ade4c6 100644
--- a/libavcodec/aac/aacdec_ac.h
+++ b/libavcodec/aac/aacdec_ac.h
@@ -25,7 +25,7 @@
#include "libavcodec/get_bits.h"
typedef struct AACArithState {
- uint8_t last[512 /* 2048 / 4 */];
+ uint8_t last[512 /* 2048 / 4 */ + 1];
int last_len;
uint8_t cur[4];
uint16_t state_pre;
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* [FFmpeg-devel] [PATCH 4/6] aacdec_usac: rename noise_scale to noise_bands
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 2/6] aac: expose ff_aac_sample_rate_idx() in aac.h Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 3/6] aacdec_ac: fix an overread Lynne via ffmpeg-devel
@ 2024-06-16 8:54 ` Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 5/6] aacdec_usac: remove custom rate_idx and use standard variable for it Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC Lynne via ffmpeg-devel
4 siblings, 0 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
This was a typo.
---
libavcodec/aac/aacdec.h | 2 +-
libavcodec/aac/aacdec_usac.c | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libavcodec/aac/aacdec.h b/libavcodec/aac/aacdec.h
index 86faf6454a..d1a80e9ac1 100644
--- a/libavcodec/aac/aacdec.h
+++ b/libavcodec/aac/aacdec.h
@@ -315,7 +315,7 @@ typedef struct AACUsacElemConfig {
uint8_t freq_scale; /* dflt_freq_scale */
uint8_t alter_scale : 1; /* dflt_alter_scale */
- uint8_t noise_scale; /* dflt_noise_scale */
+ uint8_t noise_bands; /* dflt_noise_bands */
uint8_t limiter_bands; /* dflt_limiter_bands */
uint8_t limiter_gains; /* dflt_limiter_gains */
diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
index 065bc869d9..eb0e7d3659 100644
--- a/libavcodec/aac/aacdec_usac.c
+++ b/libavcodec/aac/aacdec_usac.c
@@ -162,11 +162,11 @@ static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
e->sbr.dflt.freq_scale = 2;
e->sbr.dflt.alter_scale = 1;
- e->sbr.dflt.noise_scale = 2;
+ e->sbr.dflt.noise_bands = 2;
if (header_extra1) {
e->sbr.dflt.freq_scale = get_bits(gb, 2); /* dflt_freq_scale */
e->sbr.dflt.alter_scale = get_bits1(gb); /* dflt_alter_scale */
- e->sbr.dflt.noise_scale = get_bits(gb, 2); /* dflt_noise_scale */
+ e->sbr.dflt.noise_bands = get_bits(gb, 2); /* dflt_noise_bands */
}
e->sbr.dflt.limiter_bands = 2;
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* [FFmpeg-devel] [PATCH 5/6] aacdec_usac: remove custom rate_idx and use standard variable for it
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
` (2 preceding siblings ...)
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 4/6] aacdec_usac: rename noise_scale to noise_bands Lynne via ffmpeg-devel
@ 2024-06-16 8:54 ` Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC Lynne via ffmpeg-devel
4 siblings, 0 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
m4ac.sampling_index is what aacdec.c uses.
---
libavcodec/aac/aacdec.h | 1 -
libavcodec/aac/aacdec_usac.c | 35 ++++++++++++++---------------------
2 files changed, 14 insertions(+), 22 deletions(-)
diff --git a/libavcodec/aac/aacdec.h b/libavcodec/aac/aacdec.h
index d1a80e9ac1..e5a79a7139 100644
--- a/libavcodec/aac/aacdec.h
+++ b/libavcodec/aac/aacdec.h
@@ -350,7 +350,6 @@ typedef struct AACUsacElemConfig {
typedef struct AACUSACConfig {
uint8_t core_sbr_frame_len_idx; /* coreSbrFrameLengthIndex */
- uint8_t rate_idx;
uint16_t core_frame_len;
uint16_t stream_identifier;
diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
index eb0e7d3659..e5504117d0 100644
--- a/libavcodec/aac/aacdec_usac.c
+++ b/libavcodec/aac/aacdec_usac.c
@@ -316,7 +316,7 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
GetBitContext *gb, OutputConfiguration *oc,
int channel_config)
{
- int ret, idx;
+ int ret;
uint8_t freq_idx;
uint8_t channel_config_idx;
int nb_channels = 0;
@@ -334,20 +334,10 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */
if (freq_idx == 0x1f) {
samplerate = get_bits(gb, 24); /* usacSamplingFrequency */
-
- /* Try to match up an index for the custom sample rate.
- * TODO: not sure if correct */
- for (idx = 0; idx < /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32; idx++) {
- if (ff_aac_usac_samplerate[idx] >= samplerate)
- break;
- }
- idx = FFMIN(idx, /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32 - 1);
- usac->rate_idx = idx;
} else {
samplerate = ff_aac_usac_samplerate[freq_idx];
if (samplerate < 0)
return AVERROR(EINVAL);
- usac->rate_idx = freq_idx;
}
m4ac->sample_rate = avctx->sample_rate = samplerate;
@@ -364,6 +354,8 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
usac->core_sbr_frame_len_idx == 4 ? 1 :
0;
+ m4ac->sampling_index = ff_aac_sample_rate_idx(m4ac->sample_rate);
+
channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
if (!channel_config_idx) {
/* UsacChannelConfig() */
@@ -751,18 +743,19 @@ static int setup_sce(AACDecContext *ac, SingleChannelElement *sce,
{
AACUsacElemData *ue = &sce->ue;
IndividualChannelStream *ics = &sce->ics;
+ const int sampling_index = ac->oc[1].m4ac.sampling_index;
/* Setup window parameters */
ics->prev_num_window_groups = FFMAX(ics->num_window_groups, 1);
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
if (usac->core_frame_len == 768) {
- ics->swb_offset = ff_swb_offset_96[usac->rate_idx];
- ics->num_swb = ff_aac_num_swb_96[usac->rate_idx];
+ ics->swb_offset = ff_swb_offset_96[sampling_index];
+ ics->num_swb = ff_aac_num_swb_96[sampling_index];
} else {
- ics->swb_offset = ff_swb_offset_128[usac->rate_idx];
- ics->num_swb = ff_aac_num_swb_128[usac->rate_idx];
+ ics->swb_offset = ff_swb_offset_128[sampling_index];
+ ics->num_swb = ff_aac_num_swb_128[sampling_index];
}
- ics->tns_max_bands = ff_tns_max_bands_usac_128[usac->rate_idx];
+ ics->tns_max_bands = ff_tns_max_bands_usac_128[sampling_index];
/* Setup scalefactor grouping. 7 bit mask. */
ics->num_window_groups = 0;
@@ -779,13 +772,13 @@ static int setup_sce(AACDecContext *ac, SingleChannelElement *sce,
ics->num_windows = 8;
} else {
if (usac->core_frame_len == 768) {
- ics->swb_offset = ff_swb_offset_768[usac->rate_idx];
- ics->num_swb = ff_aac_num_swb_768[usac->rate_idx];
+ ics->swb_offset = ff_swb_offset_768[sampling_index];
+ ics->num_swb = ff_aac_num_swb_768[sampling_index];
} else {
- ics->swb_offset = ff_swb_offset_1024[usac->rate_idx];
- ics->num_swb = ff_aac_num_swb_1024[usac->rate_idx];
+ ics->swb_offset = ff_swb_offset_1024[sampling_index];
+ ics->num_swb = ff_aac_num_swb_1024[sampling_index];
}
- ics->tns_max_bands = ff_tns_max_bands_usac_1024[usac->rate_idx];
+ ics->tns_max_bands = ff_tns_max_bands_usac_1024[sampling_index];
ics->group_len[0] = 1;
ics->num_window_groups = 1;
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
` (3 preceding siblings ...)
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 5/6] aacdec_usac: remove custom rate_idx and use standard variable for it Lynne via ffmpeg-devel
@ 2024-06-16 8:54 ` Lynne via ffmpeg-devel
2024-06-17 7:35 ` Anton Khirnov
2024-06-20 2:12 ` Lynne via ffmpeg-devel
4 siblings, 2 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-16 8:54 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
Currently, no eSBR features are supported.
Thankfully, no encoders exist for it yet.
---
libavcodec/aac/aacdec_usac.c | 119 +++++++++++++++---
libavcodec/aacsbr.h | 11 ++
libavcodec/aacsbr_template.c | 232 ++++++++++++++++++++++++++++++++---
libavcodec/sbr.h | 32 +++--
4 files changed, 351 insertions(+), 43 deletions(-)
diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
index e5504117d0..132ffee9c2 100644
--- a/libavcodec/aac/aacdec_usac.c
+++ b/libavcodec/aac/aacdec_usac.c
@@ -23,6 +23,8 @@
#include "aacdec_lpd.h"
#include "aacdec_ac.h"
+#include "libavcodec/aacsbr.h"
+
#include "libavcodec/aactab.h"
#include "libavutil/mem.h"
#include "libavcodec/mpeg4audio.h"
@@ -145,7 +147,8 @@ static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac,
return 0;
}
-static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
+static int decode_usac_sbr_data(AACDecContext *ac,
+ AACUsacElemConfig *e, GetBitContext *gb)
{
uint8_t header_extra1;
uint8_t header_extra2;
@@ -153,6 +156,10 @@ static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
+ if (e->sbr.harmonic_sbr || e->sbr.bs_intertes || e->sbr.bs_pvc) {
+ avpriv_report_missing_feature(ac->avctx, "AAC USAC eSBR");
+ return AVERROR_PATCHWELCOME;
+ }
e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
@@ -179,6 +186,8 @@ static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */
e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */
}
+
+ return 0;
}
static void decode_usac_element_core(AACUsacElemConfig *e,
@@ -190,13 +199,17 @@ static void decode_usac_element_core(AACUsacElemConfig *e,
e->sbr.ratio = sbr_ratio;
}
-static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
+static int decode_usac_element_pair(AACDecContext *ac,
+ AACUsacElemConfig *e, GetBitContext *gb)
{
e->stereo_config_index = 0;
if (e->sbr.ratio) {
- decode_usac_sbr_data(e, gb);
+ int ret = decode_usac_sbr_data(ac, e, gb);
+ if (ret < 0)
+ return ret;
e->stereo_config_index = get_bits(gb, 2);
}
+
if (e->stereo_config_index) {
e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
@@ -216,6 +229,8 @@ static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
if (e->mps.temp_shape_config == 2)
e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
}
+
+ return 0;
}
static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e,
@@ -294,6 +309,9 @@ int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
AACUsacStereo *us = &che->us;
memset(us, 0, sizeof(*us));
+ if (e->sbr.ratio)
+ ff_aac_sbr_config_usac(ac, che, e);
+
for (int j = 0; j < ch; j++) {
SingleChannelElement *sce = &che->ch[ch];
AACUsacElemData *ue = &sce->ue;
@@ -320,6 +338,7 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
uint8_t freq_idx;
uint8_t channel_config_idx;
int nb_channels = 0;
+ int ratio_mult, ratio_dec;
int samplerate;
int sbr_ratio;
MPEG4AudioConfig *m4ac = &oc->m4ac;
@@ -340,8 +359,6 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
return AVERROR(EINVAL);
}
- m4ac->sample_rate = avctx->sample_rate = samplerate;
-
usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
usac->core_sbr_frame_len_idx == 2;
@@ -354,7 +371,26 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
usac->core_sbr_frame_len_idx == 4 ? 1 :
0;
+ if (sbr_ratio == 2) {
+ ratio_mult = 8;
+ ratio_dec = 3;
+ } else if (sbr_ratio == 3) {
+ ratio_mult = 2;
+ ratio_dec = 1;
+ } else if (sbr_ratio == 4) {
+ ratio_mult = 4;
+ ratio_dec = 1;
+ } else {
+ ratio_mult = 1;
+ ratio_dec = 1;
+ }
+
+ avctx->sample_rate = samplerate;
+ m4ac->ext_sample_rate = samplerate;
+ m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
+
m4ac->sampling_index = ff_aac_sample_rate_idx(m4ac->sample_rate);
+ m4ac->sbr = sbr_ratio > 0;
channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
if (!channel_config_idx) {
@@ -426,8 +462,11 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
case ID_USAC_SCE: /* SCE */
/* UsacCoreConfig */
decode_usac_element_core(e, gb, sbr_ratio);
- if (e->sbr.ratio > 0)
- decode_usac_sbr_data(e, gb);
+ if (e->sbr.ratio > 0) {
+ ret = decode_usac_sbr_data(ac, e, gb);
+ if (ret < 0)
+ return ret;
+ }
layout_map[map_count][0] = TYPE_SCE;
layout_map[map_count][1] = elem_id[0]++;
if (!map_pos_set)
@@ -437,7 +476,9 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
case ID_USAC_CPE: /* UsacChannelPairElementConf */
/* UsacCoreConfig */
decode_usac_element_core(e, gb, sbr_ratio);
- decode_usac_element_pair(e, gb);
+ ret = decode_usac_element_pair(ac, e, gb);
+ if (ret < 0)
+ return ret;
layout_map[map_count][0] = TYPE_CPE;
layout_map[map_count][1] = elem_id[1]++;
if (!map_pos_set)
@@ -1307,13 +1348,14 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
int ret;
int arith_reset_flag;
AACUsacStereo *us = &che->us;
+ int core_nb_channels = nb_channels;
/* Local symbols */
uint8_t global_gain;
us->common_window = 0;
- for (int ch = 0; ch < nb_channels; ch++) {
+ for (int ch = 0; ch < core_nb_channels; ch++) {
SingleChannelElement *sce = &che->ch[ch];
AACUsacElemData *ue = &sce->ue;
@@ -1323,13 +1365,16 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
ue->core_mode = get_bits1(gb);
}
- if (nb_channels == 2) {
+ if (nb_channels > 1 && ec->stereo_config_index == 1)
+ core_nb_channels = 1;
+
+ if (core_nb_channels == 2) {
ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
if (ret)
return ret;
}
- for (int ch = 0; ch < nb_channels; ch++) {
+ for (int ch = 0; ch < core_nb_channels; ch++) {
SingleChannelElement *sce = &che->ch[ch];
IndividualChannelStream *ics = &sce->ics;
AACUsacElemData *ue = &sce->ue;
@@ -1341,7 +1386,7 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
continue;
}
- if ((nb_channels == 1) ||
+ if ((core_nb_channels == 1) ||
(che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
ue->tns_data_present = get_bits1(gb);
@@ -1424,7 +1469,29 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
}
}
- spectrum_decode(ac, usac, che, nb_channels);
+ if (ec->sbr.ratio) {
+ int sbr_ch = nb_channels;
+ if (nb_channels == 2 &&
+ !(ec->stereo_config_index == 0 || ec->stereo_config_index == 3))
+ sbr_ch = 1;
+
+ ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch, indep_flag);
+ if (ret < 0)
+ return ret;
+
+ if (ec->stereo_config_index) {
+ avpriv_report_missing_feature(ac->avctx, "AAC USAC Mps212");
+ return AVERROR_PATCHWELCOME;
+ }
+ }
+
+ spectrum_decode(ac, usac, che, core_nb_channels);
+
+ if (ac->oc[1].m4ac.sbr > 0) {
+ ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE : TYPE_SCE,
+ che->ch[0].output,
+ che->ch[1].output);
+ }
return 0;
}
@@ -1591,9 +1658,29 @@ int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
int indep_flag, samples = 0;
int audio_found = 0;
int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
-
AVFrame *frame = ac->frame;
+ int ratio_mult, ratio_dec;
+ AACUSACConfig *usac = &ac->oc[1].usac;
+ int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
+ usac->core_sbr_frame_len_idx == 3 ? 3 :
+ usac->core_sbr_frame_len_idx == 4 ? 1 :
+ 0;
+
+ if (sbr_ratio == 2) {
+ ratio_mult = 8;
+ ratio_dec = 3;
+ } else if (sbr_ratio == 3) {
+ ratio_mult = 2;
+ ratio_dec = 1;
+ } else if (sbr_ratio == 4) {
+ ratio_mult = 4;
+ ratio_dec = 1;
+ } else {
+ ratio_mult = 1;
+ ratio_dec = 1;
+ }
+
ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
ac->oc[1].status, 0);
@@ -1660,8 +1747,10 @@ int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
if (audio_found)
samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
+ samples = (samples * ratio_mult) / ratio_dec;
+
if (ac->oc[1].status && audio_found) {
- avctx->sample_rate = ac->oc[1].m4ac.sample_rate;
+ avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
avctx->frame_size = samples;
ac->oc[1].status = OC_LOCKED;
}
diff --git a/libavcodec/aacsbr.h b/libavcodec/aacsbr.h
index d4582d1100..3958b43b91 100644
--- a/libavcodec/aacsbr.h
+++ b/libavcodec/aacsbr.h
@@ -88,6 +88,17 @@ int ff_aac_sbr_decode_extension(AACDecContext *ac, ChannelElement *che,
int ff_aac_sbr_decode_extension_fixed(AACDecContext *ac, ChannelElement *che,
GetBitContext *gb, int crc, int cnt, int id_aac);
+/** Due to channel allocation not being known upon SBR parameter transmission,
+ * supply the parameters separately.
+ * Functionally identical to ff_aac_sbr_decode_extension() */
+int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
+ AACUsacElemConfig *ue);
+
+/** Decode frame SBR data, USAC. */
+int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
+ AACUsacElemConfig *ue, GetBitContext *gb,
+ int sbr_ch, int indep_flag);
+
/** Apply one SBR element to one AAC element. */
void ff_aac_sbr_apply(AACDecContext *ac, ChannelElement *che,
int id_aac, void /* float */ *L, void /* float */ *R);
diff --git a/libavcodec/aacsbr_template.c b/libavcodec/aacsbr_template.c
index 86f4d8c26e..e5bc4d4659 100644
--- a/libavcodec/aacsbr_template.c
+++ b/libavcodec/aacsbr_template.c
@@ -57,6 +57,7 @@ av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
/** Places SBR in pure upsampling mode. */
static void sbr_turnoff(SpectralBandReplication *sbr) {
sbr->start = 0;
+ sbr->usac = 0;
sbr->ready_for_dequant = 0;
// Init defults used in pure upsampling mode
sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
@@ -184,7 +185,8 @@ static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
}
}
-static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
+static unsigned int read_sbr_header(SpectralBandReplication *sbr,
+ GetBitContext *gb, int is_usac)
{
unsigned int cnt = get_bits_count(gb);
uint8_t bs_header_extra_1;
@@ -194,15 +196,20 @@ static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext
sbr->start = 1;
sbr->ready_for_dequant = 0;
+ sbr->usac = is_usac;
// Save last spectrum parameters variables to compare to new ones
memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
- sbr->bs_amp_res_header = get_bits1(gb);
+ if (!is_usac)
+ sbr->bs_amp_res_header = get_bits1(gb);
+
sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
- sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
- skip_bits(gb, 2); // bs_reserved
+
+ if (!is_usac)
+ sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
+ skip_bits(gb, 2); // bs_reserved
bs_header_extra_1 = get_bits1(gb);
bs_header_extra_2 = get_bits1(gb);
@@ -645,7 +652,7 @@ static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr,
switch (bs_frame_class = get_bits(gb, 2)) {
case FIXFIX:
bs_num_env = 1 << get_bits(gb, 2);
- if (bs_num_env > 4) {
+ if (bs_num_env > (sbr->usac ? 8 : 5)) {
av_log(ac->avctx, AV_LOG_ERROR,
"Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
bs_num_env);
@@ -793,10 +800,26 @@ static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
/// Read how the envelope and noise floor data is delta coded
static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
- SBRData *ch_data)
+ SBRData *ch_data, int indep_flag)
{
- get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
- get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
+ if (sbr->usac) {
+ if (indep_flag) {
+ ch_data->bs_df_env[0] = 0;
+ get_bits1_vector(gb, &ch_data->bs_df_env[1], ch_data->bs_num_env - 1);
+ } else {
+ get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
+ }
+
+ if (indep_flag) {
+ ch_data->bs_df_noise[0] = 0;
+ get_bits1_vector(gb, &ch_data->bs_df_noise[1], ch_data->bs_num_noise - 1);
+ } else {
+ get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
+ }
+ } else {
+ get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
+ get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
+ }
}
/// Read inverse filtering data
@@ -811,7 +834,7 @@ static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
}
static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
- SBRData *ch_data, int ch)
+ SBRData *ch_data, int ch)
{
int bits;
int i, j, k;
@@ -881,6 +904,13 @@ static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, Ge
}
}
}
+ if (sbr->usac) {
+ if (sbr->inter_tes) {
+ ch_data->temp_shape[i] = get_bits(gb, 1);
+ if (ch_data->temp_shape[i])
+ ch_data->temp_shape_mode[i] = get_bits(gb, 2);
+ }
+ }
}
//assign 0th elements of env_facs_q from last elements
@@ -970,7 +1000,7 @@ static int read_sbr_single_channel_element(AACDecContext *ac,
if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
return -1;
- read_sbr_dtdf(sbr, gb, &sbr->data[0]);
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
read_sbr_invf(sbr, gb, &sbr->data[0]);
if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
return ret;
@@ -996,8 +1026,8 @@ static int read_sbr_channel_pair_element(AACDecContext *ac,
if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
return -1;
copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
- read_sbr_dtdf(sbr, gb, &sbr->data[0]);
- read_sbr_dtdf(sbr, gb, &sbr->data[1]);
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
+ read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
read_sbr_invf(sbr, gb, &sbr->data[0]);
memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
@@ -1013,8 +1043,8 @@ static int read_sbr_channel_pair_element(AACDecContext *ac,
if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
return -1;
- read_sbr_dtdf(sbr, gb, &sbr->data[0]);
- read_sbr_dtdf(sbr, gb, &sbr->data[1]);
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
+ read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
read_sbr_invf(sbr, gb, &sbr->data[0]);
read_sbr_invf(sbr, gb, &sbr->data[1]);
if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
@@ -1129,7 +1159,7 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
num_sbr_bits++;
if (get_bits1(gb)) // bs_header_flag
- num_sbr_bits += read_sbr_header(sbr, gb);
+ num_sbr_bits += read_sbr_header(sbr, gb, 0);
if (sbr->reset)
sbr_reset(ac, sbr);
@@ -1148,6 +1178,178 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
return cnt;
}
+#if !USE_FIXED
+static void copy_usac_default_header(SpectralBandReplication *sbr,
+ AACUsacElemConfig *ue)
+{
+ sbr->inter_tes = ue->sbr.bs_intertes;
+
+ sbr->spectrum_params.bs_start_freq = ue->sbr.dflt.start_freq;
+ sbr->spectrum_params.bs_stop_freq = ue->sbr.dflt.stop_freq;
+
+ sbr->spectrum_params.bs_freq_scale = ue->sbr.dflt.freq_scale;
+ sbr->spectrum_params.bs_alter_scale = ue->sbr.dflt.alter_scale;
+ sbr->spectrum_params.bs_noise_bands = ue->sbr.dflt.noise_bands;
+
+ sbr->bs_limiter_bands = ue->sbr.dflt.limiter_bands;
+ sbr->bs_limiter_gains = ue->sbr.dflt.limiter_gains;
+ sbr->bs_interpol_freq = ue->sbr.dflt.interpol_freq;
+ sbr->bs_smoothing_mode = ue->sbr.dflt.smoothing_mode;
+}
+
+int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
+ AACUsacElemConfig *ue)
+{
+ SpectralBandReplication *sbr = get_sbr(che);
+ sbr_turnoff(sbr);
+ return 0;
+}
+
+int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
+ AACUsacElemConfig *ue, GetBitContext *gb,
+ int sbr_ch, int indep_flag)
+{
+ int ret;
+ SpectralBandReplication *sbr = get_sbr(che);
+ int info_present = 1;
+ int header_present = 1;
+
+ sbr->reset = 0;
+ sbr->usac = 1;
+
+ sbr->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
+ sbr->id_aac = sbr_ch == 2 ? TYPE_CPE : TYPE_SCE;
+
+ if (!indep_flag) {
+ info_present = get_bits1(gb);
+ if (info_present)
+ header_present = get_bits1(gb);
+ else
+ header_present = 0;
+ }
+
+ if (info_present) {
+ /* SbrInfo() */
+ sbr->bs_amp_res_header = get_bits1(gb);
+ sbr->spectrum_params.bs_xover_band = get_bits(gb, 4);
+ sbr->bs_sbr_preprocessing = get_bits1(gb);
+ /* if (bs_pvc) ... */
+ }
+
+ if (header_present) {
+ if (get_bits1(gb)) {
+ int old_bs_limiter_bands = sbr->bs_limiter_bands;
+ SpectrumParameters old_spectrum_params;
+ memcpy(&old_spectrum_params, &sbr->spectrum_params,
+ sizeof(SpectrumParameters));
+
+ copy_usac_default_header(sbr, ue);
+ // Check if spectrum parameters changed
+ if (memcmp(&old_spectrum_params, &sbr->spectrum_params,
+ sizeof(SpectrumParameters)))
+ sbr->reset = 1;
+
+ if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
+ sbr_make_f_tablelim(sbr);
+ } else {
+ read_sbr_header(sbr, gb, 1);
+ }
+
+ sbr->start = 1;
+ }
+
+ //Save some state from the previous frame.
+ sbr->kx[0] = sbr->kx[1];
+ sbr->m[0] = sbr->m[1];
+ sbr->kx_and_m_pushed = 1;
+
+ if (sbr->reset)
+ sbr_reset(ac, sbr);
+
+ sbr->ready_for_dequant = 1;
+
+ int start = get_bits_count(gb);
+
+ if (sbr_ch == 1) { /* sbr_single_channel_element */
+ /* if (harmonicSBR) ... */
+
+ if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
+ return -1;
+
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
+ read_sbr_invf(sbr, gb, &sbr->data[0]);
+
+ if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+
+ if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+
+ if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
+ get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
+ } else if (get_bits1(gb)) { /* bs_coupling == 1 */
+ /* if (harmonicSBR) ... */
+
+ if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
+ return -1;
+ copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
+
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
+ read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
+
+ read_sbr_invf(sbr, gb, &sbr->data[0]);
+ memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0],
+ sizeof(sbr->data[1].bs_invf_mode[0]));
+ memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0],
+ sizeof(sbr->data[1].bs_invf_mode[0]));
+
+ if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+ if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+
+ if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
+ return ret;
+ if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
+ return ret;
+
+ if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
+ get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
+ if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
+ get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
+ } else { /* bs_coupling == 0 */
+ /* if (harmonicSBR) ... */
+ if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
+ return -1;
+ if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
+ return -1;
+
+ read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
+ read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
+
+ read_sbr_invf(sbr, gb, &sbr->data[0]);
+ read_sbr_invf(sbr, gb, &sbr->data[1]);
+
+ if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+ if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
+ return ret;
+
+ if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
+ return ret;
+ if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
+ return ret;
+
+ if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
+ get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
+ if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
+ get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
+ }
+
+ return 0;
+}
+#endif
+
/**
* Analysis QMF Bank (14496-3 sp04 p206)
*
diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h
index fe3a39603a..40bb30e04d 100644
--- a/libavcodec/sbr.h
+++ b/libavcodec/sbr.h
@@ -68,9 +68,9 @@ typedef struct SBRData {
unsigned bs_frame_class;
unsigned bs_add_harmonic_flag;
AAC_SIGNE bs_num_env;
- uint8_t bs_freq_res[7];
+ uint8_t bs_freq_res[9];
AAC_SIGNE bs_num_noise;
- uint8_t bs_df_env[5];
+ uint8_t bs_df_env[9];
uint8_t bs_df_noise[2];
uint8_t bs_invf_mode[2][5];
uint8_t bs_add_harmonic[48];
@@ -95,21 +95,24 @@ typedef struct SBRData {
DECLARE_ALIGNED(16, INTFLOAT, Y)[2][38][64][2];
DECLARE_ALIGNED(16, AAC_FLOAT, g_temp)[42][48];
AAC_FLOAT q_temp[42][48];
- uint8_t s_indexmapped[8][48];
+ uint8_t s_indexmapped[9][48];
///Envelope scalefactors
- uint8_t env_facs_q[6][48];
- AAC_FLOAT env_facs[6][48];
+ uint8_t env_facs_q[9][48];
+ AAC_FLOAT env_facs[9][48];
///Noise scalefactors
uint8_t noise_facs_q[3][5];
AAC_FLOAT noise_facs[3][5];
///Envelope time borders
- uint8_t t_env[8];
+ uint8_t t_env[9];
///Envelope time border of the last envelope of the previous frame
uint8_t t_env_num_env_old;
///Noise time borders
uint8_t t_q[3];
unsigned f_indexnoise;
unsigned f_indexsine;
+ //inter_tes (USAC)
+ uint8_t temp_shape[6];
+ uint8_t temp_shape_mode[6];
/** @} */
} SBRData;
@@ -142,9 +145,12 @@ struct SpectralBandReplication {
int start;
int ready_for_dequant;
int id_aac;
+ int usac;
+ int inter_tes; // USAC-only
int reset;
SpectrumParameters spectrum_params;
int bs_amp_res_header;
+ int bs_sbr_preprocessing; // USAC-only
/**
* @name Variables associated with bs_header_extra_2
* @{
@@ -196,18 +202,18 @@ struct SpectralBandReplication {
///First coefficient used to filter the subband signals
DECLARE_ALIGNED(16, INTFLOAT, alpha1)[64][2];
///Dequantized envelope scalefactors, remapped
- AAC_FLOAT e_origmapped[7][48];
+ AAC_FLOAT e_origmapped[8][48];
///Dequantized noise scalefactors, remapped
- AAC_FLOAT q_mapped[7][48];
+ AAC_FLOAT q_mapped[8][48];
///Sinusoidal presence, remapped
- uint8_t s_mapped[7][48];
+ uint8_t s_mapped[8][48];
///Estimated envelope
- AAC_FLOAT e_curr[7][48];
+ AAC_FLOAT e_curr[8][48];
///Amplitude adjusted noise scalefactors
- AAC_FLOAT q_m[7][48];
+ AAC_FLOAT q_m[8][48];
///Sinusoidal levels
- AAC_FLOAT s_m[7][48];
- AAC_FLOAT gain[7][48];
+ AAC_FLOAT s_m[8][48];
+ AAC_FLOAT gain[8][48];
DECLARE_ALIGNED(32, INTFLOAT, qmf_filter_scratch)[5][64];
AVTXContext *mdct_ana;
av_tx_fn mdct_ana_fn;
--
2.45.1.288.g0e0cd299f1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC Lynne via ffmpeg-devel
@ 2024-06-17 7:35 ` Anton Khirnov
2024-06-17 19:01 ` Lynne via ffmpeg-devel
2024-06-20 2:12 ` Lynne via ffmpeg-devel
1 sibling, 1 reply; 12+ messages in thread
From: Anton Khirnov @ 2024-06-17 7:35 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Lynne
No tests?
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-17 7:35 ` Anton Khirnov
@ 2024-06-17 19:01 ` Lynne via ffmpeg-devel
2024-06-17 19:04 ` Lynne via ffmpeg-devel
0 siblings, 1 reply; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-17 19:01 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Lynne
[-- Attachment #1.1.1.1: Type: text/plain, Size: 532 bytes --]
On 17/06/2024 09:35, Anton Khirnov wrote:
> No tests?
Tests for this particular part are tricky. We still have the SBR issue
where we add a single sample of delay to the output, which the reference
samples and their decodings do not. Adding a test would mean modifying
the samples in a way where we would not be able to use them after fixing
this (in addition to all the other regular AAC samples with SBR we have
that would become irrelevant).
My plan is to fix this after this patch, as its a troublesome topic.
[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 637 bytes --]
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-17 19:01 ` Lynne via ffmpeg-devel
@ 2024-06-17 19:04 ` Lynne via ffmpeg-devel
2024-06-26 7:29 ` Anton Khirnov
0 siblings, 1 reply; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-17 19:04 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Lynne
[-- Attachment #1.1.1.1: Type: text/plain, Size: 686 bytes --]
On 17/06/2024 21:01, Lynne wrote:
> On 17/06/2024 09:35, Anton Khirnov wrote:
>> No tests?
>
> Tests for this particular part are tricky. We still have the SBR issue
> where we add a single sample of delay to the output, which the reference
> samples and their decodings do not. Adding a test would mean modifying
> the samples in a way where we would not be able to use them after fixing
> this (in addition to all the other regular AAC samples with SBR we have
> that would become irrelevant).
>
> My plan is to fix this after this patch, as its a troublesome topic.
Additionally, we also don't correctly take into account encoder SBR
delay signalled via MP4.
[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 637 bytes --]
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC Lynne via ffmpeg-devel
2024-06-17 7:35 ` Anton Khirnov
@ 2024-06-20 2:12 ` Lynne via ffmpeg-devel
2024-06-23 7:11 ` Lynne via ffmpeg-devel
1 sibling, 1 reply; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-20 2:12 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
[-- Attachment #1.1.1.1: Type: text/plain, Size: 29769 bytes --]
On 16/06/2024 10:54, Lynne wrote:
> Currently, no eSBR features are supported.
> Thankfully, no encoders exist for it yet.
> ---
> libavcodec/aac/aacdec_usac.c | 119 +++++++++++++++---
> libavcodec/aacsbr.h | 11 ++
> libavcodec/aacsbr_template.c | 232 ++++++++++++++++++++++++++++++++---
> libavcodec/sbr.h | 32 +++--
> 4 files changed, 351 insertions(+), 43 deletions(-)
>
> diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
> index e5504117d0..132ffee9c2 100644
> --- a/libavcodec/aac/aacdec_usac.c
> +++ b/libavcodec/aac/aacdec_usac.c
> @@ -23,6 +23,8 @@
> #include "aacdec_lpd.h"
> #include "aacdec_ac.h"
>
> +#include "libavcodec/aacsbr.h"
> +
> #include "libavcodec/aactab.h"
> #include "libavutil/mem.h"
> #include "libavcodec/mpeg4audio.h"
> @@ -145,7 +147,8 @@ static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac,
> return 0;
> }
>
> -static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
> +static int decode_usac_sbr_data(AACDecContext *ac,
> + AACUsacElemConfig *e, GetBitContext *gb)
> {
> uint8_t header_extra1;
> uint8_t header_extra2;
> @@ -153,6 +156,10 @@ static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
> e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
> e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
> e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
> + if (e->sbr.harmonic_sbr || e->sbr.bs_intertes || e->sbr.bs_pvc) {
> + avpriv_report_missing_feature(ac->avctx, "AAC USAC eSBR");
> + return AVERROR_PATCHWELCOME;
> + }
>
> e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
> e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
> @@ -179,6 +186,8 @@ static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
> e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */
> e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */
> }
> +
> + return 0;
> }
>
> static void decode_usac_element_core(AACUsacElemConfig *e,
> @@ -190,13 +199,17 @@ static void decode_usac_element_core(AACUsacElemConfig *e,
> e->sbr.ratio = sbr_ratio;
> }
>
> -static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
> +static int decode_usac_element_pair(AACDecContext *ac,
> + AACUsacElemConfig *e, GetBitContext *gb)
> {
> e->stereo_config_index = 0;
> if (e->sbr.ratio) {
> - decode_usac_sbr_data(e, gb);
> + int ret = decode_usac_sbr_data(ac, e, gb);
> + if (ret < 0)
> + return ret;
> e->stereo_config_index = get_bits(gb, 2);
> }
> +
> if (e->stereo_config_index) {
> e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
> e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
> @@ -216,6 +229,8 @@ static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
> if (e->mps.temp_shape_config == 2)
> e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
> }
> +
> + return 0;
> }
>
> static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e,
> @@ -294,6 +309,9 @@ int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
> AACUsacStereo *us = &che->us;
> memset(us, 0, sizeof(*us));
>
> + if (e->sbr.ratio)
> + ff_aac_sbr_config_usac(ac, che, e);
> +
> for (int j = 0; j < ch; j++) {
> SingleChannelElement *sce = &che->ch[ch];
> AACUsacElemData *ue = &sce->ue;
> @@ -320,6 +338,7 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
> uint8_t freq_idx;
> uint8_t channel_config_idx;
> int nb_channels = 0;
> + int ratio_mult, ratio_dec;
> int samplerate;
> int sbr_ratio;
> MPEG4AudioConfig *m4ac = &oc->m4ac;
> @@ -340,8 +359,6 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
> return AVERROR(EINVAL);
> }
>
> - m4ac->sample_rate = avctx->sample_rate = samplerate;
> -
> usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
> m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
> usac->core_sbr_frame_len_idx == 2;
> @@ -354,7 +371,26 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
> usac->core_sbr_frame_len_idx == 4 ? 1 :
> 0;
>
> + if (sbr_ratio == 2) {
> + ratio_mult = 8;
> + ratio_dec = 3;
> + } else if (sbr_ratio == 3) {
> + ratio_mult = 2;
> + ratio_dec = 1;
> + } else if (sbr_ratio == 4) {
> + ratio_mult = 4;
> + ratio_dec = 1;
> + } else {
> + ratio_mult = 1;
> + ratio_dec = 1;
> + }
> +
> + avctx->sample_rate = samplerate;
> + m4ac->ext_sample_rate = samplerate;
> + m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
> +
> m4ac->sampling_index = ff_aac_sample_rate_idx(m4ac->sample_rate);
> + m4ac->sbr = sbr_ratio > 0;
>
> channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
> if (!channel_config_idx) {
> @@ -426,8 +462,11 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
> case ID_USAC_SCE: /* SCE */
> /* UsacCoreConfig */
> decode_usac_element_core(e, gb, sbr_ratio);
> - if (e->sbr.ratio > 0)
> - decode_usac_sbr_data(e, gb);
> + if (e->sbr.ratio > 0) {
> + ret = decode_usac_sbr_data(ac, e, gb);
> + if (ret < 0)
> + return ret;
> + }
> layout_map[map_count][0] = TYPE_SCE;
> layout_map[map_count][1] = elem_id[0]++;
> if (!map_pos_set)
> @@ -437,7 +476,9 @@ int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
> case ID_USAC_CPE: /* UsacChannelPairElementConf */
> /* UsacCoreConfig */
> decode_usac_element_core(e, gb, sbr_ratio);
> - decode_usac_element_pair(e, gb);
> + ret = decode_usac_element_pair(ac, e, gb);
> + if (ret < 0)
> + return ret;
> layout_map[map_count][0] = TYPE_CPE;
> layout_map[map_count][1] = elem_id[1]++;
> if (!map_pos_set)
> @@ -1307,13 +1348,14 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
> int ret;
> int arith_reset_flag;
> AACUsacStereo *us = &che->us;
> + int core_nb_channels = nb_channels;
>
> /* Local symbols */
> uint8_t global_gain;
>
> us->common_window = 0;
>
> - for (int ch = 0; ch < nb_channels; ch++) {
> + for (int ch = 0; ch < core_nb_channels; ch++) {
> SingleChannelElement *sce = &che->ch[ch];
> AACUsacElemData *ue = &sce->ue;
>
> @@ -1323,13 +1365,16 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
> ue->core_mode = get_bits1(gb);
> }
>
> - if (nb_channels == 2) {
> + if (nb_channels > 1 && ec->stereo_config_index == 1)
> + core_nb_channels = 1;
> +
> + if (core_nb_channels == 2) {
> ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
> if (ret)
> return ret;
> }
>
> - for (int ch = 0; ch < nb_channels; ch++) {
> + for (int ch = 0; ch < core_nb_channels; ch++) {
> SingleChannelElement *sce = &che->ch[ch];
> IndividualChannelStream *ics = &sce->ics;
> AACUsacElemData *ue = &sce->ue;
> @@ -1341,7 +1386,7 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
> continue;
> }
>
> - if ((nb_channels == 1) ||
> + if ((core_nb_channels == 1) ||
> (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
> ue->tns_data_present = get_bits1(gb);
>
> @@ -1424,7 +1469,29 @@ static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
> }
> }
>
> - spectrum_decode(ac, usac, che, nb_channels);
> + if (ec->sbr.ratio) {
> + int sbr_ch = nb_channels;
> + if (nb_channels == 2 &&
> + !(ec->stereo_config_index == 0 || ec->stereo_config_index == 3))
> + sbr_ch = 1;
> +
> + ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch, indep_flag);
> + if (ret < 0)
> + return ret;
> +
> + if (ec->stereo_config_index) {
> + avpriv_report_missing_feature(ac->avctx, "AAC USAC Mps212");
> + return AVERROR_PATCHWELCOME;
> + }
> + }
> +
> + spectrum_decode(ac, usac, che, core_nb_channels);
> +
> + if (ac->oc[1].m4ac.sbr > 0) {
> + ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE : TYPE_SCE,
> + che->ch[0].output,
> + che->ch[1].output);
> + }
>
> return 0;
> }
> @@ -1591,9 +1658,29 @@ int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
> int indep_flag, samples = 0;
> int audio_found = 0;
> int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
> -
> AVFrame *frame = ac->frame;
>
> + int ratio_mult, ratio_dec;
> + AACUSACConfig *usac = &ac->oc[1].usac;
> + int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
> + usac->core_sbr_frame_len_idx == 3 ? 3 :
> + usac->core_sbr_frame_len_idx == 4 ? 1 :
> + 0;
> +
> + if (sbr_ratio == 2) {
> + ratio_mult = 8;
> + ratio_dec = 3;
> + } else if (sbr_ratio == 3) {
> + ratio_mult = 2;
> + ratio_dec = 1;
> + } else if (sbr_ratio == 4) {
> + ratio_mult = 4;
> + ratio_dec = 1;
> + } else {
> + ratio_mult = 1;
> + ratio_dec = 1;
> + }
> +
> ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
> ac->oc[1].status, 0);
>
> @@ -1660,8 +1747,10 @@ int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
> if (audio_found)
> samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
>
> + samples = (samples * ratio_mult) / ratio_dec;
> +
> if (ac->oc[1].status && audio_found) {
> - avctx->sample_rate = ac->oc[1].m4ac.sample_rate;
> + avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
> avctx->frame_size = samples;
> ac->oc[1].status = OC_LOCKED;
> }
> diff --git a/libavcodec/aacsbr.h b/libavcodec/aacsbr.h
> index d4582d1100..3958b43b91 100644
> --- a/libavcodec/aacsbr.h
> +++ b/libavcodec/aacsbr.h
> @@ -88,6 +88,17 @@ int ff_aac_sbr_decode_extension(AACDecContext *ac, ChannelElement *che,
> int ff_aac_sbr_decode_extension_fixed(AACDecContext *ac, ChannelElement *che,
> GetBitContext *gb, int crc, int cnt, int id_aac);
>
> +/** Due to channel allocation not being known upon SBR parameter transmission,
> + * supply the parameters separately.
> + * Functionally identical to ff_aac_sbr_decode_extension() */
> +int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
> + AACUsacElemConfig *ue);
> +
> +/** Decode frame SBR data, USAC. */
> +int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
> + AACUsacElemConfig *ue, GetBitContext *gb,
> + int sbr_ch, int indep_flag);
> +
> /** Apply one SBR element to one AAC element. */
> void ff_aac_sbr_apply(AACDecContext *ac, ChannelElement *che,
> int id_aac, void /* float */ *L, void /* float */ *R);
> diff --git a/libavcodec/aacsbr_template.c b/libavcodec/aacsbr_template.c
> index 86f4d8c26e..e5bc4d4659 100644
> --- a/libavcodec/aacsbr_template.c
> +++ b/libavcodec/aacsbr_template.c
> @@ -57,6 +57,7 @@ av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
> /** Places SBR in pure upsampling mode. */
> static void sbr_turnoff(SpectralBandReplication *sbr) {
> sbr->start = 0;
> + sbr->usac = 0;
> sbr->ready_for_dequant = 0;
> // Init defults used in pure upsampling mode
> sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
> @@ -184,7 +185,8 @@ static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
> }
> }
>
> -static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
> +static unsigned int read_sbr_header(SpectralBandReplication *sbr,
> + GetBitContext *gb, int is_usac)
> {
> unsigned int cnt = get_bits_count(gb);
> uint8_t bs_header_extra_1;
> @@ -194,15 +196,20 @@ static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext
>
> sbr->start = 1;
> sbr->ready_for_dequant = 0;
> + sbr->usac = is_usac;
>
> // Save last spectrum parameters variables to compare to new ones
> memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
>
> - sbr->bs_amp_res_header = get_bits1(gb);
> + if (!is_usac)
> + sbr->bs_amp_res_header = get_bits1(gb);
> +
> sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
> sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
> - sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
> - skip_bits(gb, 2); // bs_reserved
> +
> + if (!is_usac)
> + sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
> + skip_bits(gb, 2); // bs_reserved
>
> bs_header_extra_1 = get_bits1(gb);
> bs_header_extra_2 = get_bits1(gb);
> @@ -645,7 +652,7 @@ static int read_sbr_grid(AACDecContext *ac, SpectralBandReplication *sbr,
> switch (bs_frame_class = get_bits(gb, 2)) {
> case FIXFIX:
> bs_num_env = 1 << get_bits(gb, 2);
> - if (bs_num_env > 4) {
> + if (bs_num_env > (sbr->usac ? 8 : 5)) {
> av_log(ac->avctx, AV_LOG_ERROR,
> "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
> bs_num_env);
> @@ -793,10 +800,26 @@ static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
>
> /// Read how the envelope and noise floor data is delta coded
> static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
> - SBRData *ch_data)
> + SBRData *ch_data, int indep_flag)
> {
> - get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
> - get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
> + if (sbr->usac) {
> + if (indep_flag) {
> + ch_data->bs_df_env[0] = 0;
> + get_bits1_vector(gb, &ch_data->bs_df_env[1], ch_data->bs_num_env - 1);
> + } else {
> + get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
> + }
> +
> + if (indep_flag) {
> + ch_data->bs_df_noise[0] = 0;
> + get_bits1_vector(gb, &ch_data->bs_df_noise[1], ch_data->bs_num_noise - 1);
> + } else {
> + get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
> + }
> + } else {
> + get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
> + get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
> + }
> }
>
> /// Read inverse filtering data
> @@ -811,7 +834,7 @@ static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
> }
>
> static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, GetBitContext *gb,
> - SBRData *ch_data, int ch)
> + SBRData *ch_data, int ch)
> {
> int bits;
> int i, j, k;
> @@ -881,6 +904,13 @@ static int read_sbr_envelope(AACDecContext *ac, SpectralBandReplication *sbr, Ge
> }
> }
> }
> + if (sbr->usac) {
> + if (sbr->inter_tes) {
> + ch_data->temp_shape[i] = get_bits(gb, 1);
> + if (ch_data->temp_shape[i])
> + ch_data->temp_shape_mode[i] = get_bits(gb, 2);
> + }
> + }
> }
>
> //assign 0th elements of env_facs_q from last elements
> @@ -970,7 +1000,7 @@ static int read_sbr_single_channel_element(AACDecContext *ac,
>
> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
> return -1;
> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
> read_sbr_invf(sbr, gb, &sbr->data[0]);
> if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> return ret;
> @@ -996,8 +1026,8 @@ static int read_sbr_channel_pair_element(AACDecContext *ac,
> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
> return -1;
> copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
> - read_sbr_dtdf(sbr, gb, &sbr->data[1]);
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
> + read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
> read_sbr_invf(sbr, gb, &sbr->data[0]);
> memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
> memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
> @@ -1013,8 +1043,8 @@ static int read_sbr_channel_pair_element(AACDecContext *ac,
> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
> read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
> return -1;
> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
> - read_sbr_dtdf(sbr, gb, &sbr->data[1]);
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
> + read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
> read_sbr_invf(sbr, gb, &sbr->data[0]);
> read_sbr_invf(sbr, gb, &sbr->data[1]);
> if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> @@ -1129,7 +1159,7 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
>
> num_sbr_bits++;
> if (get_bits1(gb)) // bs_header_flag
> - num_sbr_bits += read_sbr_header(sbr, gb);
> + num_sbr_bits += read_sbr_header(sbr, gb, 0);
>
> if (sbr->reset)
> sbr_reset(ac, sbr);
> @@ -1148,6 +1178,178 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)(AACDecContext *ac, ChannelElement *c
> return cnt;
> }
>
> +#if !USE_FIXED
> +static void copy_usac_default_header(SpectralBandReplication *sbr,
> + AACUsacElemConfig *ue)
> +{
> + sbr->inter_tes = ue->sbr.bs_intertes;
> +
> + sbr->spectrum_params.bs_start_freq = ue->sbr.dflt.start_freq;
> + sbr->spectrum_params.bs_stop_freq = ue->sbr.dflt.stop_freq;
> +
> + sbr->spectrum_params.bs_freq_scale = ue->sbr.dflt.freq_scale;
> + sbr->spectrum_params.bs_alter_scale = ue->sbr.dflt.alter_scale;
> + sbr->spectrum_params.bs_noise_bands = ue->sbr.dflt.noise_bands;
> +
> + sbr->bs_limiter_bands = ue->sbr.dflt.limiter_bands;
> + sbr->bs_limiter_gains = ue->sbr.dflt.limiter_gains;
> + sbr->bs_interpol_freq = ue->sbr.dflt.interpol_freq;
> + sbr->bs_smoothing_mode = ue->sbr.dflt.smoothing_mode;
> +}
> +
> +int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
> + AACUsacElemConfig *ue)
> +{
> + SpectralBandReplication *sbr = get_sbr(che);
> + sbr_turnoff(sbr);
> + return 0;
> +}
> +
> +int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
> + AACUsacElemConfig *ue, GetBitContext *gb,
> + int sbr_ch, int indep_flag)
> +{
> + int ret;
> + SpectralBandReplication *sbr = get_sbr(che);
> + int info_present = 1;
> + int header_present = 1;
> +
> + sbr->reset = 0;
> + sbr->usac = 1;
> +
> + sbr->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
> + sbr->id_aac = sbr_ch == 2 ? TYPE_CPE : TYPE_SCE;
> +
> + if (!indep_flag) {
> + info_present = get_bits1(gb);
> + if (info_present)
> + header_present = get_bits1(gb);
> + else
> + header_present = 0;
> + }
> +
> + if (info_present) {
> + /* SbrInfo() */
> + sbr->bs_amp_res_header = get_bits1(gb);
> + sbr->spectrum_params.bs_xover_band = get_bits(gb, 4);
> + sbr->bs_sbr_preprocessing = get_bits1(gb);
> + /* if (bs_pvc) ... */
> + }
> +
> + if (header_present) {
> + if (get_bits1(gb)) {
> + int old_bs_limiter_bands = sbr->bs_limiter_bands;
> + SpectrumParameters old_spectrum_params;
> + memcpy(&old_spectrum_params, &sbr->spectrum_params,
> + sizeof(SpectrumParameters));
> +
> + copy_usac_default_header(sbr, ue);
> + // Check if spectrum parameters changed
> + if (memcmp(&old_spectrum_params, &sbr->spectrum_params,
> + sizeof(SpectrumParameters)))
> + sbr->reset = 1;
> +
> + if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
> + sbr_make_f_tablelim(sbr);
> + } else {
> + read_sbr_header(sbr, gb, 1);
> + }
> +
> + sbr->start = 1;
> + }
> +
> + //Save some state from the previous frame.
> + sbr->kx[0] = sbr->kx[1];
> + sbr->m[0] = sbr->m[1];
> + sbr->kx_and_m_pushed = 1;
> +
> + if (sbr->reset)
> + sbr_reset(ac, sbr);
> +
> + sbr->ready_for_dequant = 1;
> +
> + int start = get_bits_count(gb);
> +
> + if (sbr_ch == 1) { /* sbr_single_channel_element */
> + /* if (harmonicSBR) ... */
> +
> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
> + return -1;
> +
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
> + read_sbr_invf(sbr, gb, &sbr->data[0]);
> +
> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> +
> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> +
> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
> + } else if (get_bits1(gb)) { /* bs_coupling == 1 */
> + /* if (harmonicSBR) ... */
> +
> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
> + return -1;
> + copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
> +
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
> + read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
> +
> + read_sbr_invf(sbr, gb, &sbr->data[0]);
> + memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0],
> + sizeof(sbr->data[1].bs_invf_mode[0]));
> + memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0],
> + sizeof(sbr->data[1].bs_invf_mode[0]));
> +
> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> +
> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
> + return ret;
> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
> + return ret;
> +
> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
> + if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
> + get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
> + } else { /* bs_coupling == 0 */
> + /* if (harmonicSBR) ... */
> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
> + return -1;
> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
> + return -1;
> +
> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
> + read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
> +
> + read_sbr_invf(sbr, gb, &sbr->data[0]);
> + read_sbr_invf(sbr, gb, &sbr->data[1]);
> +
> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1)) < 0)
> + return ret;
> +
> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
> + return ret;
> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
> + return ret;
> +
> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
> + if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
> + get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
> + }
> +
> + return 0;
> +}
> +#endif
> +
> /**
> * Analysis QMF Bank (14496-3 sp04 p206)
> *
> diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h
> index fe3a39603a..40bb30e04d 100644
> --- a/libavcodec/sbr.h
> +++ b/libavcodec/sbr.h
> @@ -68,9 +68,9 @@ typedef struct SBRData {
> unsigned bs_frame_class;
> unsigned bs_add_harmonic_flag;
> AAC_SIGNE bs_num_env;
> - uint8_t bs_freq_res[7];
> + uint8_t bs_freq_res[9];
> AAC_SIGNE bs_num_noise;
> - uint8_t bs_df_env[5];
> + uint8_t bs_df_env[9];
> uint8_t bs_df_noise[2];
> uint8_t bs_invf_mode[2][5];
> uint8_t bs_add_harmonic[48];
> @@ -95,21 +95,24 @@ typedef struct SBRData {
> DECLARE_ALIGNED(16, INTFLOAT, Y)[2][38][64][2];
> DECLARE_ALIGNED(16, AAC_FLOAT, g_temp)[42][48];
> AAC_FLOAT q_temp[42][48];
> - uint8_t s_indexmapped[8][48];
> + uint8_t s_indexmapped[9][48];
> ///Envelope scalefactors
> - uint8_t env_facs_q[6][48];
> - AAC_FLOAT env_facs[6][48];
> + uint8_t env_facs_q[9][48];
> + AAC_FLOAT env_facs[9][48];
> ///Noise scalefactors
> uint8_t noise_facs_q[3][5];
> AAC_FLOAT noise_facs[3][5];
> ///Envelope time borders
> - uint8_t t_env[8];
> + uint8_t t_env[9];
> ///Envelope time border of the last envelope of the previous frame
> uint8_t t_env_num_env_old;
> ///Noise time borders
> uint8_t t_q[3];
> unsigned f_indexnoise;
> unsigned f_indexsine;
> + //inter_tes (USAC)
> + uint8_t temp_shape[6];
> + uint8_t temp_shape_mode[6];
> /** @} */
> } SBRData;
>
> @@ -142,9 +145,12 @@ struct SpectralBandReplication {
> int start;
> int ready_for_dequant;
> int id_aac;
> + int usac;
> + int inter_tes; // USAC-only
> int reset;
> SpectrumParameters spectrum_params;
> int bs_amp_res_header;
> + int bs_sbr_preprocessing; // USAC-only
> /**
> * @name Variables associated with bs_header_extra_2
> * @{
> @@ -196,18 +202,18 @@ struct SpectralBandReplication {
> ///First coefficient used to filter the subband signals
> DECLARE_ALIGNED(16, INTFLOAT, alpha1)[64][2];
> ///Dequantized envelope scalefactors, remapped
> - AAC_FLOAT e_origmapped[7][48];
> + AAC_FLOAT e_origmapped[8][48];
> ///Dequantized noise scalefactors, remapped
> - AAC_FLOAT q_mapped[7][48];
> + AAC_FLOAT q_mapped[8][48];
> ///Sinusoidal presence, remapped
> - uint8_t s_mapped[7][48];
> + uint8_t s_mapped[8][48];
> ///Estimated envelope
> - AAC_FLOAT e_curr[7][48];
> + AAC_FLOAT e_curr[8][48];
> ///Amplitude adjusted noise scalefactors
> - AAC_FLOAT q_m[7][48];
> + AAC_FLOAT q_m[8][48];
> ///Sinusoidal levels
> - AAC_FLOAT s_m[7][48];
> - AAC_FLOAT gain[7][48];
> + AAC_FLOAT s_m[8][48];
> + AAC_FLOAT gain[8][48];
> DECLARE_ALIGNED(32, INTFLOAT, qmf_filter_scratch)[5][64];
> AVTXContext *mdct_ana;
> av_tx_fn mdct_ana_fn;
I'll wait 2 more days before merging this patch, but I'll push the other
5 fix/prep patches tomorrow.
[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 637 bytes --]
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-20 2:12 ` Lynne via ffmpeg-devel
@ 2024-06-23 7:11 ` Lynne via ffmpeg-devel
0 siblings, 0 replies; 12+ messages in thread
From: Lynne via ffmpeg-devel @ 2024-06-23 7:11 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Lynne
[-- Attachment #1.1.1.1: Type: text/plain, Size: 35279 bytes --]
On 20/06/2024 04:12, Lynne wrote:
> On 16/06/2024 10:54, Lynne wrote:
>> Currently, no eSBR features are supported.
>> Thankfully, no encoders exist for it yet.
>> ---
>> libavcodec/aac/aacdec_usac.c | 119 +++++++++++++++---
>> libavcodec/aacsbr.h | 11 ++
>> libavcodec/aacsbr_template.c | 232 ++++++++++++++++++++++++++++++++---
>> libavcodec/sbr.h | 32 +++--
>> 4 files changed, 351 insertions(+), 43 deletions(-)
>>
>> diff --git a/libavcodec/aac/aacdec_usac.c b/libavcodec/aac/aacdec_usac.c
>> index e5504117d0..132ffee9c2 100644
>> --- a/libavcodec/aac/aacdec_usac.c
>> +++ b/libavcodec/aac/aacdec_usac.c
>> @@ -23,6 +23,8 @@
>> #include "aacdec_lpd.h"
>> #include "aacdec_ac.h"
>> +#include "libavcodec/aacsbr.h"
>> +
>> #include "libavcodec/aactab.h"
>> #include "libavutil/mem.h"
>> #include "libavcodec/mpeg4audio.h"
>> @@ -145,7 +147,8 @@ static int decode_loudness_set(AACDecContext *ac,
>> AACUSACConfig *usac,
>> return 0;
>> }
>> -static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext
>> *gb)
>> +static int decode_usac_sbr_data(AACDecContext *ac,
>> + AACUsacElemConfig *e, GetBitContext *gb)
>> {
>> uint8_t header_extra1;
>> uint8_t header_extra2;
>> @@ -153,6 +156,10 @@ static void
>> decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
>> e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
>> e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
>> e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
>> + if (e->sbr.harmonic_sbr || e->sbr.bs_intertes || e->sbr.bs_pvc) {
>> + avpriv_report_missing_feature(ac->avctx, "AAC USAC eSBR");
>> + return AVERROR_PATCHWELCOME;
>> + }
>> e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
>> e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
>> @@ -179,6 +186,8 @@ static void decode_usac_sbr_data(AACUsacElemConfig
>> *e, GetBitContext *gb)
>> e->sbr.dflt.interpol_freq = get_bits1(gb); /*
>> dflt_interpol_freq */
>> e->sbr.dflt.smoothing_mode = get_bits1(gb); /*
>> dflt_smoothing_mode */
>> }
>> +
>> + return 0;
>> }
>> static void decode_usac_element_core(AACUsacElemConfig *e,
>> @@ -190,13 +199,17 @@ static void
>> decode_usac_element_core(AACUsacElemConfig *e,
>> e->sbr.ratio = sbr_ratio;
>> }
>> -static void decode_usac_element_pair(AACUsacElemConfig *e,
>> GetBitContext *gb)
>> +static int decode_usac_element_pair(AACDecContext *ac,
>> + AACUsacElemConfig *e,
>> GetBitContext *gb)
>> {
>> e->stereo_config_index = 0;
>> if (e->sbr.ratio) {
>> - decode_usac_sbr_data(e, gb);
>> + int ret = decode_usac_sbr_data(ac, e, gb);
>> + if (ret < 0)
>> + return ret;
>> e->stereo_config_index = get_bits(gb, 2);
>> }
>> +
>> if (e->stereo_config_index) {
>> e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
>> e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
>> @@ -216,6 +229,8 @@ static void
>> decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
>> if (e->mps.temp_shape_config == 2)
>> e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
>> }
>> +
>> + return 0;
>> }
>> static int decode_usac_extension(AACDecContext *ac,
>> AACUsacElemConfig *e,
>> @@ -294,6 +309,9 @@ int ff_aac_usac_reset_state(AACDecContext *ac,
>> OutputConfiguration *oc)
>> AACUsacStereo *us = &che->us;
>> memset(us, 0, sizeof(*us));
>> + if (e->sbr.ratio)
>> + ff_aac_sbr_config_usac(ac, che, e);
>> +
>> for (int j = 0; j < ch; j++) {
>> SingleChannelElement *sce = &che->ch[ch];
>> AACUsacElemData *ue = &sce->ue;
>> @@ -320,6 +338,7 @@ int ff_aac_usac_config_decode(AACDecContext *ac,
>> AVCodecContext *avctx,
>> uint8_t freq_idx;
>> uint8_t channel_config_idx;
>> int nb_channels = 0;
>> + int ratio_mult, ratio_dec;
>> int samplerate;
>> int sbr_ratio;
>> MPEG4AudioConfig *m4ac = &oc->m4ac;
>> @@ -340,8 +359,6 @@ int ff_aac_usac_config_decode(AACDecContext *ac,
>> AVCodecContext *avctx,
>> return AVERROR(EINVAL);
>> }
>> - m4ac->sample_rate = avctx->sample_rate = samplerate;
>> -
>> usac->core_sbr_frame_len_idx = get_bits(gb, 3); /*
>> coreSbrFrameLengthIndex */
>> m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
>> usac->core_sbr_frame_len_idx == 2;
>> @@ -354,7 +371,26 @@ int ff_aac_usac_config_decode(AACDecContext *ac,
>> AVCodecContext *avctx,
>> usac->core_sbr_frame_len_idx == 4 ? 1 :
>> 0;
>> + if (sbr_ratio == 2) {
>> + ratio_mult = 8;
>> + ratio_dec = 3;
>> + } else if (sbr_ratio == 3) {
>> + ratio_mult = 2;
>> + ratio_dec = 1;
>> + } else if (sbr_ratio == 4) {
>> + ratio_mult = 4;
>> + ratio_dec = 1;
>> + } else {
>> + ratio_mult = 1;
>> + ratio_dec = 1;
>> + }
>> +
>> + avctx->sample_rate = samplerate;
>> + m4ac->ext_sample_rate = samplerate;
>> + m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
>> +
>> m4ac->sampling_index = ff_aac_sample_rate_idx(m4ac->sample_rate);
>> + m4ac->sbr = sbr_ratio > 0;
>> channel_config_idx = get_bits(gb, 5); /*
>> channelConfigurationIndex */
>> if (!channel_config_idx) {
>> @@ -426,8 +462,11 @@ int ff_aac_usac_config_decode(AACDecContext *ac,
>> AVCodecContext *avctx,
>> case ID_USAC_SCE: /* SCE */
>> /* UsacCoreConfig */
>> decode_usac_element_core(e, gb, sbr_ratio);
>> - if (e->sbr.ratio > 0)
>> - decode_usac_sbr_data(e, gb);
>> + if (e->sbr.ratio > 0) {
>> + ret = decode_usac_sbr_data(ac, e, gb);
>> + if (ret < 0)
>> + return ret;
>> + }
>> layout_map[map_count][0] = TYPE_SCE;
>> layout_map[map_count][1] = elem_id[0]++;
>> if (!map_pos_set)
>> @@ -437,7 +476,9 @@ int ff_aac_usac_config_decode(AACDecContext *ac,
>> AVCodecContext *avctx,
>> case ID_USAC_CPE: /* UsacChannelPairElementConf */
>> /* UsacCoreConfig */
>> decode_usac_element_core(e, gb, sbr_ratio);
>> - decode_usac_element_pair(e, gb);
>> + ret = decode_usac_element_pair(ac, e, gb);
>> + if (ret < 0)
>> + return ret;
>> layout_map[map_count][0] = TYPE_CPE;
>> layout_map[map_count][1] = elem_id[1]++;
>> if (!map_pos_set)
>> @@ -1307,13 +1348,14 @@ static int
>> decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
>> int ret;
>> int arith_reset_flag;
>> AACUsacStereo *us = &che->us;
>> + int core_nb_channels = nb_channels;
>> /* Local symbols */
>> uint8_t global_gain;
>> us->common_window = 0;
>> - for (int ch = 0; ch < nb_channels; ch++) {
>> + for (int ch = 0; ch < core_nb_channels; ch++) {
>> SingleChannelElement *sce = &che->ch[ch];
>> AACUsacElemData *ue = &sce->ue;
>> @@ -1323,13 +1365,16 @@ static int
>> decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
>> ue->core_mode = get_bits1(gb);
>> }
>> - if (nb_channels == 2) {
>> + if (nb_channels > 1 && ec->stereo_config_index == 1)
>> + core_nb_channels = 1;
>> +
>> + if (core_nb_channels == 2) {
>> ret = decode_usac_stereo_info(ac, usac, ec, che, gb,
>> indep_flag);
>> if (ret)
>> return ret;
>> }
>> - for (int ch = 0; ch < nb_channels; ch++) {
>> + for (int ch = 0; ch < core_nb_channels; ch++) {
>> SingleChannelElement *sce = &che->ch[ch];
>> IndividualChannelStream *ics = &sce->ics;
>> AACUsacElemData *ue = &sce->ue;
>> @@ -1341,7 +1386,7 @@ static int decode_usac_core_coder(AACDecContext
>> *ac, AACUSACConfig *usac,
>> continue;
>> }
>> - if ((nb_channels == 1) ||
>> + if ((core_nb_channels == 1) ||
>> (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
>> ue->tns_data_present = get_bits1(gb);
>> @@ -1424,7 +1469,29 @@ static int decode_usac_core_coder(AACDecContext
>> *ac, AACUSACConfig *usac,
>> }
>> }
>> - spectrum_decode(ac, usac, che, nb_channels);
>> + if (ec->sbr.ratio) {
>> + int sbr_ch = nb_channels;
>> + if (nb_channels == 2 &&
>> + !(ec->stereo_config_index == 0 || ec->stereo_config_index
>> == 3))
>> + sbr_ch = 1;
>> +
>> + ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch,
>> indep_flag);
>> + if (ret < 0)
>> + return ret;
>> +
>> + if (ec->stereo_config_index) {
>> + avpriv_report_missing_feature(ac->avctx, "AAC USAC Mps212");
>> + return AVERROR_PATCHWELCOME;
>> + }
>> + }
>> +
>> + spectrum_decode(ac, usac, che, core_nb_channels);
>> +
>> + if (ac->oc[1].m4ac.sbr > 0) {
>> + ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE :
>> TYPE_SCE,
>> + che->ch[0].output,
>> + che->ch[1].output);
>> + }
>> return 0;
>> }
>> @@ -1591,9 +1658,29 @@ int ff_aac_usac_decode_frame(AVCodecContext
>> *avctx, AACDecContext *ac,
>> int indep_flag, samples = 0;
>> int audio_found = 0;
>> int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
>> -
>> AVFrame *frame = ac->frame;
>> + int ratio_mult, ratio_dec;
>> + AACUSACConfig *usac = &ac->oc[1].usac;
>> + int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
>> + usac->core_sbr_frame_len_idx == 3 ? 3 :
>> + usac->core_sbr_frame_len_idx == 4 ? 1 :
>> + 0;
>> +
>> + if (sbr_ratio == 2) {
>> + ratio_mult = 8;
>> + ratio_dec = 3;
>> + } else if (sbr_ratio == 3) {
>> + ratio_mult = 2;
>> + ratio_dec = 1;
>> + } else if (sbr_ratio == 4) {
>> + ratio_mult = 4;
>> + ratio_dec = 1;
>> + } else {
>> + ratio_mult = 1;
>> + ratio_dec = 1;
>> + }
>> +
>> ff_aac_output_configure(ac, ac->oc[1].layout_map, ac-
>> >oc[1].layout_map_tags,
>> ac->oc[1].status, 0);
>> @@ -1660,8 +1747,10 @@ int ff_aac_usac_decode_frame(AVCodecContext
>> *avctx, AACDecContext *ac,
>> if (audio_found)
>> samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
>> + samples = (samples * ratio_mult) / ratio_dec;
>> +
>> if (ac->oc[1].status && audio_found) {
>> - avctx->sample_rate = ac->oc[1].m4ac.sample_rate;
>> + avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
>> avctx->frame_size = samples;
>> ac->oc[1].status = OC_LOCKED;
>> }
>> diff --git a/libavcodec/aacsbr.h b/libavcodec/aacsbr.h
>> index d4582d1100..3958b43b91 100644
>> --- a/libavcodec/aacsbr.h
>> +++ b/libavcodec/aacsbr.h
>> @@ -88,6 +88,17 @@ int ff_aac_sbr_decode_extension(AACDecContext *ac,
>> ChannelElement *che,
>> int ff_aac_sbr_decode_extension_fixed(AACDecContext *ac,
>> ChannelElement *che,
>> GetBitContext *gb, int crc,
>> int cnt, int id_aac);
>> +/** Due to channel allocation not being known upon SBR parameter
>> transmission,
>> + * supply the parameters separately.
>> + * Functionally identical to ff_aac_sbr_decode_extension() */
>> +int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
>> + AACUsacElemConfig *ue);
>> +
>> +/** Decode frame SBR data, USAC. */
>> +int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
>> + AACUsacElemConfig *ue, GetBitContext
>> *gb,
>> + int sbr_ch, int indep_flag);
>> +
>> /** Apply one SBR element to one AAC element. */
>> void ff_aac_sbr_apply(AACDecContext *ac, ChannelElement *che,
>> int id_aac, void /* float */ *L, void /* float
>> */ *R);
>> diff --git a/libavcodec/aacsbr_template.c b/libavcodec/aacsbr_template.c
>> index 86f4d8c26e..e5bc4d4659 100644
>> --- a/libavcodec/aacsbr_template.c
>> +++ b/libavcodec/aacsbr_template.c
>> @@ -57,6 +57,7 @@ av_cold void AAC_RENAME(ff_aac_sbr_init)(void)
>> /** Places SBR in pure upsampling mode. */
>> static void sbr_turnoff(SpectralBandReplication *sbr) {
>> sbr->start = 0;
>> + sbr->usac = 0;
>> sbr->ready_for_dequant = 0;
>> // Init defults used in pure upsampling mode
>> sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
>> @@ -184,7 +185,8 @@ static void
>> sbr_make_f_tablelim(SpectralBandReplication *sbr)
>> }
>> }
>> -static unsigned int read_sbr_header(SpectralBandReplication *sbr,
>> GetBitContext *gb)
>> +static unsigned int read_sbr_header(SpectralBandReplication *sbr,
>> + GetBitContext *gb, int is_usac)
>> {
>> unsigned int cnt = get_bits_count(gb);
>> uint8_t bs_header_extra_1;
>> @@ -194,15 +196,20 @@ static unsigned int
>> read_sbr_header(SpectralBandReplication *sbr, GetBitContext
>> sbr->start = 1;
>> sbr->ready_for_dequant = 0;
>> + sbr->usac = is_usac;
>> // Save last spectrum parameters variables to compare to new ones
>> memcpy(&old_spectrum_params, &sbr->spectrum_params,
>> sizeof(SpectrumParameters));
>> - sbr->bs_amp_res_header = get_bits1(gb);
>> + if (!is_usac)
>> + sbr->bs_amp_res_header = get_bits1(gb);
>> +
>> sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
>> sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
>> - sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
>> - skip_bits(gb, 2); //
>> bs_reserved
>> +
>> + if (!is_usac)
>> + sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
>> + skip_bits(gb, 2); //
>> bs_reserved
>> bs_header_extra_1 = get_bits1(gb);
>> bs_header_extra_2 = get_bits1(gb);
>> @@ -645,7 +652,7 @@ static int read_sbr_grid(AACDecContext *ac,
>> SpectralBandReplication *sbr,
>> switch (bs_frame_class = get_bits(gb, 2)) {
>> case FIXFIX:
>> bs_num_env = 1 << get_bits(gb, 2);
>> - if (bs_num_env > 4) {
>> + if (bs_num_env > (sbr->usac ? 8 : 5)) {
>> av_log(ac->avctx, AV_LOG_ERROR,
>> "Invalid bitstream, too many SBR envelopes in
>> FIXFIX type SBR frame: %d\n",
>> bs_num_env);
>> @@ -793,10 +800,26 @@ static void copy_sbr_grid(SBRData *dst, const
>> SBRData *src) {
>> /// Read how the envelope and noise floor data is delta coded
>> static void read_sbr_dtdf(SpectralBandReplication *sbr,
>> GetBitContext *gb,
>> - SBRData *ch_data)
>> + SBRData *ch_data, int indep_flag)
>> {
>> - get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
>> - get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
>> + if (sbr->usac) {
>> + if (indep_flag) {
>> + ch_data->bs_df_env[0] = 0;
>> + get_bits1_vector(gb, &ch_data->bs_df_env[1], ch_data-
>> >bs_num_env - 1);
>> + } else {
>> + get_bits1_vector(gb, ch_data->bs_df_env, ch_data-
>> >bs_num_env);
>> + }
>> +
>> + if (indep_flag) {
>> + ch_data->bs_df_noise[0] = 0;
>> + get_bits1_vector(gb, &ch_data->bs_df_noise[1], ch_data-
>> >bs_num_noise - 1);
>> + } else {
>> + get_bits1_vector(gb, ch_data->bs_df_noise, ch_data-
>> >bs_num_noise);
>> + }
>> + } else {
>> + get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
>> + get_bits1_vector(gb, ch_data->bs_df_noise, ch_data-
>> >bs_num_noise);
>> + }
>> }
>> /// Read inverse filtering data
>> @@ -811,7 +834,7 @@ static void read_sbr_invf(SpectralBandReplication
>> *sbr, GetBitContext *gb,
>> }
>> static int read_sbr_envelope(AACDecContext *ac,
>> SpectralBandReplication *sbr, GetBitContext *gb,
>> - SBRData *ch_data, int ch)
>> + SBRData *ch_data, int ch)
>> {
>> int bits;
>> int i, j, k;
>> @@ -881,6 +904,13 @@ static int read_sbr_envelope(AACDecContext *ac,
>> SpectralBandReplication *sbr, Ge
>> }
>> }
>> }
>> + if (sbr->usac) {
>> + if (sbr->inter_tes) {
>> + ch_data->temp_shape[i] = get_bits(gb, 1);
>> + if (ch_data->temp_shape[i])
>> + ch_data->temp_shape_mode[i] = get_bits(gb, 2);
>> + }
>> + }
>> }
>> //assign 0th elements of env_facs_q from last elements
>> @@ -970,7 +1000,7 @@ static int
>> read_sbr_single_channel_element(AACDecContext *ac,
>> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
>> return -1;
>> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
>> read_sbr_invf(sbr, gb, &sbr->data[0]);
>> if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0)) < 0)
>> return ret;
>> @@ -996,8 +1026,8 @@ static int
>> read_sbr_channel_pair_element(AACDecContext *ac,
>> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
>> return -1;
>> copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
>> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
>> - read_sbr_dtdf(sbr, gb, &sbr->data[1]);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
>> read_sbr_invf(sbr, gb, &sbr->data[0]);
>> memcpy(sbr->data[1].bs_invf_mode[1], sbr-
>> >data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
>> memcpy(sbr->data[1].bs_invf_mode[0], sbr-
>> >data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
>> @@ -1013,8 +1043,8 @@ static int
>> read_sbr_channel_pair_element(AACDecContext *ac,
>> if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
>> read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
>> return -1;
>> - read_sbr_dtdf(sbr, gb, &sbr->data[0]);
>> - read_sbr_dtdf(sbr, gb, &sbr->data[1]);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], 0);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[1], 0);
>> read_sbr_invf(sbr, gb, &sbr->data[0]);
>> read_sbr_invf(sbr, gb, &sbr->data[1]);
>> if((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0))
>> < 0)
>> @@ -1129,7 +1159,7 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)
>> (AACDecContext *ac, ChannelElement *c
>> num_sbr_bits++;
>> if (get_bits1(gb)) // bs_header_flag
>> - num_sbr_bits += read_sbr_header(sbr, gb);
>> + num_sbr_bits += read_sbr_header(sbr, gb, 0);
>> if (sbr->reset)
>> sbr_reset(ac, sbr);
>> @@ -1148,6 +1178,178 @@ int AAC_RENAME(ff_aac_sbr_decode_extension)
>> (AACDecContext *ac, ChannelElement *c
>> return cnt;
>> }
>> +#if !USE_FIXED
>> +static void copy_usac_default_header(SpectralBandReplication *sbr,
>> + AACUsacElemConfig *ue)
>> +{
>> + sbr->inter_tes = ue->sbr.bs_intertes;
>> +
>> + sbr->spectrum_params.bs_start_freq = ue->sbr.dflt.start_freq;
>> + sbr->spectrum_params.bs_stop_freq = ue->sbr.dflt.stop_freq;
>> +
>> + sbr->spectrum_params.bs_freq_scale = ue->sbr.dflt.freq_scale;
>> + sbr->spectrum_params.bs_alter_scale = ue->sbr.dflt.alter_scale;
>> + sbr->spectrum_params.bs_noise_bands = ue->sbr.dflt.noise_bands;
>> +
>> + sbr->bs_limiter_bands = ue->sbr.dflt.limiter_bands;
>> + sbr->bs_limiter_gains = ue->sbr.dflt.limiter_gains;
>> + sbr->bs_interpol_freq = ue->sbr.dflt.interpol_freq;
>> + sbr->bs_smoothing_mode = ue->sbr.dflt.smoothing_mode;
>> +}
>> +
>> +int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che,
>> + AACUsacElemConfig *ue)
>> +{
>> + SpectralBandReplication *sbr = get_sbr(che);
>> + sbr_turnoff(sbr);
>> + return 0;
>> +}
>> +
>> +int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che,
>> + AACUsacElemConfig *ue, GetBitContext
>> *gb,
>> + int sbr_ch, int indep_flag)
>> +{
>> + int ret;
>> + SpectralBandReplication *sbr = get_sbr(che);
>> + int info_present = 1;
>> + int header_present = 1;
>> +
>> + sbr->reset = 0;
>> + sbr->usac = 1;
>> +
>> + sbr->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
>> + sbr->id_aac = sbr_ch == 2 ? TYPE_CPE : TYPE_SCE;
>> +
>> + if (!indep_flag) {
>> + info_present = get_bits1(gb);
>> + if (info_present)
>> + header_present = get_bits1(gb);
>> + else
>> + header_present = 0;
>> + }
>> +
>> + if (info_present) {
>> + /* SbrInfo() */
>> + sbr->bs_amp_res_header = get_bits1(gb);
>> + sbr->spectrum_params.bs_xover_band = get_bits(gb, 4);
>> + sbr->bs_sbr_preprocessing = get_bits1(gb);
>> + /* if (bs_pvc) ... */
>> + }
>> +
>> + if (header_present) {
>> + if (get_bits1(gb)) {
>> + int old_bs_limiter_bands = sbr->bs_limiter_bands;
>> + SpectrumParameters old_spectrum_params;
>> + memcpy(&old_spectrum_params, &sbr->spectrum_params,
>> + sizeof(SpectrumParameters));
>> +
>> + copy_usac_default_header(sbr, ue);
>> + // Check if spectrum parameters changed
>> + if (memcmp(&old_spectrum_params, &sbr->spectrum_params,
>> + sizeof(SpectrumParameters)))
>> + sbr->reset = 1;
>> +
>> + if (sbr->bs_limiter_bands != old_bs_limiter_bands && !
>> sbr->reset)
>> + sbr_make_f_tablelim(sbr);
>> + } else {
>> + read_sbr_header(sbr, gb, 1);
>> + }
>> +
>> + sbr->start = 1;
>> + }
>> +
>> + //Save some state from the previous frame.
>> + sbr->kx[0] = sbr->kx[1];
>> + sbr->m[0] = sbr->m[1];
>> + sbr->kx_and_m_pushed = 1;
>> +
>> + if (sbr->reset)
>> + sbr_reset(ac, sbr);
>> +
>> + sbr->ready_for_dequant = 1;
>> +
>> + int start = get_bits_count(gb);
>> +
>> + if (sbr_ch == 1) { /* sbr_single_channel_element */
>> + /* if (harmonicSBR) ... */
>> +
>> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
>> + return -1;
>> +
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
>> + read_sbr_invf(sbr, gb, &sbr->data[0]);
>> +
>> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0))
>> < 0)
>> + return ret;
>> +
>> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
>> + return ret;
>> +
>> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
>> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr-
>> >n[1]);
>> + } else if (get_bits1(gb)) { /* bs_coupling == 1 */
>> + /* if (harmonicSBR) ... */
>> +
>> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
>> + return -1;
>> + copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
>> +
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
>> +
>> + read_sbr_invf(sbr, gb, &sbr->data[0]);
>> + memcpy(sbr->data[1].bs_invf_mode[1], sbr-
>> >data[1].bs_invf_mode[0],
>> + sizeof(sbr->data[1].bs_invf_mode[0]));
>> + memcpy(sbr->data[1].bs_invf_mode[0], sbr-
>> >data[0].bs_invf_mode[0],
>> + sizeof(sbr->data[1].bs_invf_mode[0]));
>> +
>> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0))
>> < 0)
>> + return ret;
>> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
>> + return ret;
>> +
>> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1))
>> < 0)
>> + return ret;
>> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
>> + return ret;
>> +
>> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
>> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr-
>> >n[1]);
>> + if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
>> + get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr-
>> >n[1]);
>> + } else { /* bs_coupling == 0 */
>> + /* if (harmonicSBR) ... */
>> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
>> + return -1;
>> + if (read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
>> + return -1;
>> +
>> + read_sbr_dtdf(sbr, gb, &sbr->data[0], indep_flag);
>> + read_sbr_dtdf(sbr, gb, &sbr->data[1], indep_flag);
>> +
>> + read_sbr_invf(sbr, gb, &sbr->data[0]);
>> + read_sbr_invf(sbr, gb, &sbr->data[1]);
>> +
>> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[0], 0))
>> < 0)
>> + return ret;
>> + if ((ret = read_sbr_envelope(ac, sbr, gb, &sbr->data[1], 1))
>> < 0)
>> + return ret;
>> +
>> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[0], 0)) < 0)
>> + return ret;
>> + if ((ret = read_sbr_noise(ac, sbr, gb, &sbr->data[1], 1)) < 0)
>> + return ret;
>> +
>> + if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
>> + get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr-
>> >n[1]);
>> + if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
>> + get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr-
>> >n[1]);
>> + }
>> +
>> + return 0;
>> +}
>> +#endif
>> +
>> /**
>> * Analysis QMF Bank (14496-3 sp04 p206)
>> *
>> diff --git a/libavcodec/sbr.h b/libavcodec/sbr.h
>> index fe3a39603a..40bb30e04d 100644
>> --- a/libavcodec/sbr.h
>> +++ b/libavcodec/sbr.h
>> @@ -68,9 +68,9 @@ typedef struct SBRData {
>> unsigned bs_frame_class;
>> unsigned bs_add_harmonic_flag;
>> AAC_SIGNE bs_num_env;
>> - uint8_t bs_freq_res[7];
>> + uint8_t bs_freq_res[9];
>> AAC_SIGNE bs_num_noise;
>> - uint8_t bs_df_env[5];
>> + uint8_t bs_df_env[9];
>> uint8_t bs_df_noise[2];
>> uint8_t bs_invf_mode[2][5];
>> uint8_t bs_add_harmonic[48];
>> @@ -95,21 +95,24 @@ typedef struct SBRData {
>> DECLARE_ALIGNED(16, INTFLOAT, Y)[2][38][64][2];
>> DECLARE_ALIGNED(16, AAC_FLOAT, g_temp)[42][48];
>> AAC_FLOAT q_temp[42][48];
>> - uint8_t s_indexmapped[8][48];
>> + uint8_t s_indexmapped[9][48];
>> ///Envelope scalefactors
>> - uint8_t env_facs_q[6][48];
>> - AAC_FLOAT env_facs[6][48];
>> + uint8_t env_facs_q[9][48];
>> + AAC_FLOAT env_facs[9][48];
>> ///Noise scalefactors
>> uint8_t noise_facs_q[3][5];
>> AAC_FLOAT noise_facs[3][5];
>> ///Envelope time borders
>> - uint8_t t_env[8];
>> + uint8_t t_env[9];
>> ///Envelope time border of the last envelope of the previous frame
>> uint8_t t_env_num_env_old;
>> ///Noise time borders
>> uint8_t t_q[3];
>> unsigned f_indexnoise;
>> unsigned f_indexsine;
>> + //inter_tes (USAC)
>> + uint8_t temp_shape[6];
>> + uint8_t temp_shape_mode[6];
>> /** @} */
>> } SBRData;
>> @@ -142,9 +145,12 @@ struct SpectralBandReplication {
>> int start;
>> int ready_for_dequant;
>> int id_aac;
>> + int usac;
>> + int inter_tes; // USAC-only
>> int reset;
>> SpectrumParameters spectrum_params;
>> int bs_amp_res_header;
>> + int bs_sbr_preprocessing; // USAC-only
>> /**
>> * @name Variables associated with bs_header_extra_2
>> * @{
>> @@ -196,18 +202,18 @@ struct SpectralBandReplication {
>> ///First coefficient used to filter the subband signals
>> DECLARE_ALIGNED(16, INTFLOAT, alpha1)[64][2];
>> ///Dequantized envelope scalefactors, remapped
>> - AAC_FLOAT e_origmapped[7][48];
>> + AAC_FLOAT e_origmapped[8][48];
>> ///Dequantized noise scalefactors, remapped
>> - AAC_FLOAT q_mapped[7][48];
>> + AAC_FLOAT q_mapped[8][48];
>> ///Sinusoidal presence, remapped
>> - uint8_t s_mapped[7][48];
>> + uint8_t s_mapped[8][48];
>> ///Estimated envelope
>> - AAC_FLOAT e_curr[7][48];
>> + AAC_FLOAT e_curr[8][48];
>> ///Amplitude adjusted noise scalefactors
>> - AAC_FLOAT q_m[7][48];
>> + AAC_FLOAT q_m[8][48];
>> ///Sinusoidal levels
>> - AAC_FLOAT s_m[7][48];
>> - AAC_FLOAT gain[7][48];
>> + AAC_FLOAT s_m[8][48];
>> + AAC_FLOAT gain[8][48];
>> DECLARE_ALIGNED(32, INTFLOAT, qmf_filter_scratch)[5][64];
>> AVTXContext *mdct_ana;
>> av_tx_fn mdct_ana_fn;
>
> I'll wait 2 more days before merging this patch, but I'll push the other
> 5 fix/prep patches tomorrow.
Last patch pushed.
[-- Attachment #1.1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 637 bytes --]
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC
2024-06-17 19:04 ` Lynne via ffmpeg-devel
@ 2024-06-26 7:29 ` Anton Khirnov
0 siblings, 0 replies; 12+ messages in thread
From: Anton Khirnov @ 2024-06-26 7:29 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Lynne
Quoting Lynne via ffmpeg-devel (2024-06-17 21:04:30)
> On 17/06/2024 21:01, Lynne wrote:
> > On 17/06/2024 09:35, Anton Khirnov wrote:
> >> No tests?
> >
> > Tests for this particular part are tricky. We still have the SBR issue
> > where we add a single sample of delay to the output, which the reference
> > samples and their decodings do not. Adding a test would mean modifying
> > the samples in a way where we would not be able to use them after fixing
> > this (in addition to all the other regular AAC samples with SBR we have
> > that would become irrelevant).
> >
> > My plan is to fix this after this patch, as its a troublesome topic.
>
> Additionally, we also don't correctly take into account encoder SBR
> delay signalled via MP4.
Imperfect tests are better than no tests, just note their deficiencies
in a comment next to the test target.
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2024-06-26 7:29 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-16 8:54 [FFmpeg-devel] [PATCH 1/6] aacdec_usac: apply specification fix M55715 Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 2/6] aac: expose ff_aac_sample_rate_idx() in aac.h Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 3/6] aacdec_ac: fix an overread Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 4/6] aacdec_usac: rename noise_scale to noise_bands Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 5/6] aacdec_usac: remove custom rate_idx and use standard variable for it Lynne via ffmpeg-devel
2024-06-16 8:54 ` [FFmpeg-devel] [PATCH 6/6] aacdec_usac, aacsbr: implement SBR support for USAC Lynne via ffmpeg-devel
2024-06-17 7:35 ` Anton Khirnov
2024-06-17 19:01 ` Lynne via ffmpeg-devel
2024-06-17 19:04 ` Lynne via ffmpeg-devel
2024-06-26 7:29 ` Anton Khirnov
2024-06-20 2:12 ` Lynne via ffmpeg-devel
2024-06-23 7:11 ` Lynne via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git