History

aacdec: add a decoder for AAC USAC (xHE-AAC)

This commit adds a decoder for the frequency-domain part of USAC. What works: - Mono - Stereo (no prediction) - Stereo (mid/side coding) - Stereo (complex prediction) What's left: - SBR - Speech coding Known issues: - Desync with certain sequences - Preroll crossover missing (shouldn't matter, bitrate adaptation only)

Lynne committed 2 years ago

Commit eee5fa01 parent 23b45d713 files · +2510 −76Browse files

Showing the changes to libavcodec/aac (11 of 13 files). Show all

Unified · Split

11 files
  1. libavcodec/aac/Makefile +2 −1
  2. libavcodec/aac/aacdec.c +121 −67
  3. libavcodec/aac/aacdec.h +187 −0
  4. libavcodec/aac/aacdec_ac.c +208 −0
  5. libavcodec/aac/aacdec_ac.h +54 −0
  6. libavcodec/aac/aacdec_dsp_template.c +2 −2
  7. libavcodec/aac/aacdec_latm.h +8 −6
  8. libavcodec/aac/aacdec_lpd.c +198 −0
  9. libavcodec/aac/aacdec_lpd.h +33 −0
  10. libavcodec/aac/aacdec_usac.c +1608 −0
  11. libavcodec/aac/aacdec_usac.h +37 −0
libavcodec/aac/Makefile+2−1
@@ -2,6 +2,7 @@ clean:: · 1 unmodified line
2 $(RM) $(CLEANSUFFIXES:%=libavcodec/aac/%)2 $(RM) $(CLEANSUFFIXES:%=libavcodec/aac/%)
3 3
4OBJS-$(CONFIG_AAC_DECODER) += aac/aacdec.o aac/aacdec_tab.o \4OBJS-$(CONFIG_AAC_DECODER) += aac/aacdec.o aac/aacdec_tab.o \
5 aac/aacdec_float.o5 aac/aacdec_float.o aac/aacdec_usac.o \
6 aac/aacdec_ac.o aac/aacdec_lpd.o
6OBJS-$(CONFIG_AAC_FIXED_DECODER) += aac/aacdec.o aac/aacdec_tab.o \7OBJS-$(CONFIG_AAC_FIXED_DECODER) += aac/aacdec.o aac/aacdec_tab.o \
7 aac/aacdec_fixed.o8 aac/aacdec_fixed.o
libavcodec/aac/aacdec.c+121−67
@@ -40,6 +40,7 @@ · 39 unmodified lines
40 40
41#include "aacdec.h"41#include "aacdec.h"
42#include "aacdec_tab.h"42#include "aacdec_tab.h"
43#include "aacdec_usac.h"
43 44
44#include "libavcodec/aac.h"45#include "libavcodec/aac.h"
45#include "libavcodec/aac_defines.h"46#include "libavcodec/aac_defines.h"
@@ -535,6 +536,8 @@ static av_cold void flush(AVCodecContext *avctx) · 489 unmodified lines
535 }536 }
536 }537 }
537 }538 }
539
540 ff_aac_usac_reset_state(ac, &ac->oc[1]);
538}541}
539 542
540/**543/**
@@ -993,13 +996,14 @@ static int decode_eld_specific_config(AACDecContext *ac, AVCodecContext *avctx, · 452 unmodified lines
993 */996 */
994static int decode_audio_specific_config_gb(AACDecContext *ac,997static int decode_audio_specific_config_gb(AACDecContext *ac,
995 AVCodecContext *avctx,998 AVCodecContext *avctx,
996 MPEG4AudioConfig *m4ac,999 OutputConfiguration *oc,
997 GetBitContext *gb,1000 GetBitContext *gb,
998 int get_bit_alignment,1001 int get_bit_alignment,
999 int sync_extension)1002 int sync_extension)
1000{1003{
1001 int i, ret;1004 int i, ret;
1002 GetBitContext gbc = *gb;1005 GetBitContext gbc = *gb;
1006 MPEG4AudioConfig *m4ac = &oc->m4ac;
1003 MPEG4AudioConfig m4ac_bak = *m4ac;1007 MPEG4AudioConfig m4ac_bak = *m4ac;
1004 1008
1005 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {1009 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
@@ -1033,14 +1037,22 @@ static int decode_audio_specific_config_gb(AACDecContext *ac, · 27 unmodified lines
1033 case AOT_ER_AAC_LC:1037 case AOT_ER_AAC_LC:
1034 case AOT_ER_AAC_LD:1038 case AOT_ER_AAC_LD:
1035 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,1039 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1036 m4ac, m4ac->chan_config)) < 0)1040 &oc->m4ac, m4ac->chan_config)) < 0)
1037 return ret;1041 return ret;
1038 break;1042 break;
1039 case AOT_ER_AAC_ELD:1043 case AOT_ER_AAC_ELD:
1040 if ((ret = decode_eld_specific_config(ac, avctx, gb,1044 if ((ret = decode_eld_specific_config(ac, avctx, gb,
1041 m4ac, m4ac->chan_config)) < 0)1045 &oc->m4ac, m4ac->chan_config)) < 0)
1046 return ret;
1047 break;
1048#if CONFIG_AAC_DECODER
1049 case AOT_USAC_NOSBR: /* fallthrough */
1050 case AOT_USAC:
1051 if ((ret = ff_aac_usac_config_decode(ac, avctx, gb,
1052 oc, m4ac->chan_config)) < 0)
1042 return ret;1053 return ret;
1043 break;1054 break;
1055#endif
1044 default:1056 default:
1045 avpriv_report_missing_feature(avctx,1057 avpriv_report_missing_feature(avctx,
1046 "Audio object type %s%d",1058 "Audio object type %s%d",
@@ -1060,7 +1072,7 @@ static int decode_audio_specific_config_gb(AACDecContext *ac, · 13 unmodified lines
1060 1072
1061static int decode_audio_specific_config(AACDecContext *ac,1073static int decode_audio_specific_config(AACDecContext *ac,
1062 AVCodecContext *avctx,1074 AVCodecContext *avctx,
1063 MPEG4AudioConfig *m4ac,1075 OutputConfiguration *oc,
1064 const uint8_t *data, int64_t bit_size,1076 const uint8_t *data, int64_t bit_size,
1065 int sync_extension)1077 int sync_extension)
1066{1078{
@@ -1080,7 +1092,7 @@ static int decode_audio_specific_config(AACDecContext *ac, · 13 unmodified lines
1080 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)1092 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1081 return ret;1093 return ret;
1082 1094
1083 return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,1095 return decode_audio_specific_config_gb(ac, avctx, oc, &gb, 0,
1084 sync_extension);1096 sync_extension);
1085}1097}
1086 1098
@@ -1104,6 +1116,15 @@ static av_cold int decode_close(AVCodecContext *avctx) · 17 unmodified lines
1104{1116{
1105 AACDecContext *ac = avctx->priv_data;1117 AACDecContext *ac = avctx->priv_data;
1106 1118
1119 for (int i = 0; i < 2; i++) {
1120 OutputConfiguration *oc = &ac->oc[i];
1121 AACUSACConfig *usac = &oc->usac;
1122 for (int j = 0; j < usac->nb_elems; j++) {
1123 AACUsacElemConfig *ec = &usac->elems[i];
1124 av_freep(&ec->ext.pl_data);
1125 }
1126 }
1127
1107 for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {1128 for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {
1108 for (int i = 0; i < MAX_ELEM_ID; i++) {1129 for (int i = 0; i < MAX_ELEM_ID; i++) {
1109 if (ac->che[type][i]) {1130 if (ac->che[type][i]) {
@@ -1181,7 +1202,7 @@ av_cold int ff_aac_decode_init(AVCodecContext *avctx) · 71 unmodified lines
1181 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;1202 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1182 1203
1183 if (avctx->extradata_size > 0) {1204 if (avctx->extradata_size > 0) {
1184 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,1205 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
1185 avctx->extradata,1206 avctx->extradata,
1186 avctx->extradata_size * 8LL,1207 avctx->extradata_size * 8LL,
1187 1)) < 0)1208 1)) < 0)
@@ -1549,9 +1570,16 @@ static int decode_pulses(Pulse *pulse, GetBitContext *gb, · 361 unmodified lines
1549int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns,1570int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns,
1550 GetBitContext *gb, const IndividualChannelStream *ics)1571 GetBitContext *gb, const IndividualChannelStream *ics)
1551{1572{
1573 int tns_max_order = INT32_MAX;
1574 const int is_usac = ac->oc[1].m4ac.object_type == AOT_USAC ||
1575 ac->oc[1].m4ac.object_type == AOT_USAC_NOSBR;
1552 int w, filt, i, coef_len, coef_res, coef_compress;1576 int w, filt, i, coef_len, coef_res, coef_compress;
1553 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;1577 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1554 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;1578
1579 /* USAC doesn't seem to have a limit */
1580 if (!is_usac)
1581 tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1582
1555 for (w = 0; w < ics->num_windows; w++) {1583 for (w = 0; w < ics->num_windows; w++) {
1556 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {1584 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1557 coef_res = get_bits1(gb);1585 coef_res = get_bits1(gb);
@@ -1560,7 +1588,12 @@ int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns, · 2 unmodified lines
1560 int tmp2_idx;1588 int tmp2_idx;
1561 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);1589 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1562 1590
1563 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {1591 if (is_usac)
1592 tns->order[w][filt] = get_bits(gb, 4 - is8);
1593 else
1594 tns->order[w][filt] = get_bits(gb, 5 - (2 * is8));
1595
1596 if (tns->order[w][filt] > tns_max_order) {
1564 av_log(ac->avctx, AV_LOG_ERROR,1597 av_log(ac->avctx, AV_LOG_ERROR,
1565 "TNS filter order %d is greater than maximum %d.\n",1598 "TNS filter order %d is greater than maximum %d.\n",
1566 tns->order[w][filt], tns_max_order);1599 tns->order[w][filt], tns_max_order);
@@ -1598,6 +1631,7 @@ static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb, · 31 unmodified lines
1598{1631{
1599 int idx;1632 int idx;
1600 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;1633 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1634 cpe->max_sfb_ste = cpe->ch[0].ics.max_sfb;
1601 if (ms_present == 1) {1635 if (ms_present == 1) {
1602 for (idx = 0; idx < max_idx; idx++)1636 for (idx = 0; idx < max_idx; idx++)
1603 cpe->ms_mask[idx] = get_bits1(gb);1637 cpe->ms_mask[idx] = get_bits1(gb);
@@ -2182,42 +2216,19 @@ static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, · 578 unmodified lines
2182 return 0;2216 return 0;
2183}2217}
2184 2218
2185static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame,2219static int decode_frame_ga(AVCodecContext *avctx, AACDecContext *ac,
2186 int *got_frame_ptr, GetBitContext *gb,2220 GetBitContext *gb, int *got_frame_ptr)
2187 const AVPacket *avpkt)
2188{2221{
2189 AACDecContext *ac = avctx->priv_data;2222 int err;
2190 ChannelElement *che = NULL, *che_prev = NULL;2223 int is_dmono;
2224 int elem_id;
2191 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;2225 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
2192 int err, elem_id;
2193 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2194 int is_dmono, sce_count = 0;
2195 int payload_alignment;
2196 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};2226 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
2227 ChannelElement *che = NULL, *che_prev = NULL;
2228 int samples = 0, multiplier, audio_found = 0, pce_found = 0, sce_count = 0;
2229 AVFrame *frame = ac->frame;
2197 2230
2198 ac->frame = frame;2231 int payload_alignment = get_bits_count(gb);
2199
2200 if (show_bits(gb, 12) == 0xfff) {
2201 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2202 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2203 goto fail;
2204 }
2205 if (ac->oc[1].m4ac.sampling_index > 12) {
2206 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2207 err = AVERROR_INVALIDDATA;
2208 goto fail;
2209 }
2210 }
2211
2212 if ((err = frame_configure_elements(avctx)) < 0)
2213 goto fail;
2214
2215 // The AV_PROFILE_AAC_* defines are all object_type - 1
2216 // This may lead to an undefined profile being signaled
2217 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2218
2219 payload_alignment = get_bits_count(gb);
2220 ac->tags_mapped = 0;
2221 // parse2232 // parse
2222 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {2233 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2223 elem_id = get_bits(gb, 4);2234 elem_id = get_bits(gb, 4);
@@ -2225,28 +2236,23 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 1 unmodified line
2225 if (avctx->debug & FF_DEBUG_STARTCODE)2236 if (avctx->debug & FF_DEBUG_STARTCODE)
2226 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);2237 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2227 2238
2228 if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE) {2239 if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE)
2229 err = AVERROR_INVALIDDATA;2240 return AVERROR_INVALIDDATA;
2230 goto fail;
2231 }
2232 2241
2233 if (elem_type < TYPE_DSE) {2242 if (elem_type < TYPE_DSE) {
2234 if (che_presence[elem_type][elem_id]) {2243 if (che_presence[elem_type][elem_id]) {
2235 int error = che_presence[elem_type][elem_id] > 1;2244 int error = che_presence[elem_type][elem_id] > 1;
2236 av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",2245 av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
2237 elem_type, elem_id);2246 elem_type, elem_id);
2238 if (error) {2247 if (error)
2239 err = AVERROR_INVALIDDATA;2248 return AVERROR_INVALIDDATA;
2240 goto fail;
2241 }
2242 }2249 }
2243 che_presence[elem_type][elem_id]++;2250 che_presence[elem_type][elem_id]++;
2244 2251
2245 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {2252 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2246 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",2253 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2247 elem_type, elem_id);2254 elem_type, elem_id);
2248 err = AVERROR_INVALIDDATA;2255 return AVERROR_INVALIDDATA;
2249 goto fail;
2250 }2256 }
2251 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;2257 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
2252 che->present = 1;2258 che->present = 1;
@@ -2283,10 +2289,8 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 30 unmodified lines
2283 int tags;2289 int tags;
2284 2290
2285 int pushed = push_output_configuration(ac);2291 int pushed = push_output_configuration(ac);
2286 if (pce_found && !pushed) {2292 if (pce_found && !pushed)
2287 err = AVERROR_INVALIDDATA;2293 return AVERROR_INVALIDDATA;
2288 goto fail;
2289 }
2290 2294
2291 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,2295 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
2292 payload_alignment);2296 payload_alignment);
@@ -2312,8 +2316,7 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 19 unmodified lines
2312 elem_id += get_bits(gb, 8) - 1;2316 elem_id += get_bits(gb, 8) - 1;
2313 if (get_bits_left(gb) < 8 * elem_id) {2317 if (get_bits_left(gb) < 8 * elem_id) {
2314 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);2318 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2315 err = AVERROR_INVALIDDATA;2319 return AVERROR_INVALIDDATA;
2316 goto fail;
2317 }2320 }
2318 err = 0;2321 err = 0;
2319 while (elem_id > 0) {2322 while (elem_id > 0) {
@@ -2337,19 +2340,16 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 17 unmodified lines
2337 }2340 }
2338 2341
2339 if (err)2342 if (err)
2340 goto fail;2343 return err;
2341 2344
2342 if (get_bits_left(gb) < 3) {2345 if (get_bits_left(gb) < 3) {
2343 av_log(avctx, AV_LOG_ERROR, overread_err);2346 av_log(avctx, AV_LOG_ERROR, overread_err);
2344 err = AVERROR_INVALIDDATA;2347 return AVERROR_INVALIDDATA;
2345 goto fail;
2346 }2348 }
2347 }2349 }
2348 2350
2349 if (!avctx->ch_layout.nb_channels) {2351 if (!avctx->ch_layout.nb_channels)
2350 *got_frame_ptr = 0;
2351 return 0;2352 return 0;
2352 }
2353 2353
2354 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;2354 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2355 samples <<= multiplier;2355 samples <<= multiplier;
@@ -2364,16 +2364,17 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 8 unmodified lines
2364 2364
2365 if (!ac->frame->data[0] && samples) {2365 if (!ac->frame->data[0] && samples) {
2366 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");2366 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2367 err = AVERROR_INVALIDDATA;2367 return AVERROR_INVALIDDATA;
2368 goto fail;
2369 }2368 }
2370 2369
2371 if (samples) {2370 if (samples) {
2372 ac->frame->nb_samples = samples;2371 ac->frame->nb_samples = samples;
2373 ac->frame->sample_rate = avctx->sample_rate;2372 ac->frame->sample_rate = avctx->sample_rate;
2374 } else2373 *got_frame_ptr = 1;
2374 } else {
2375 av_frame_unref(ac->frame);2375 av_frame_unref(ac->frame);
2376 *got_frame_ptr = !!samples;2376 *got_frame_ptr = 0;
2377 }
2377 2378
2378 /* for dual-mono audio (SCE + SCE) */2379 /* for dual-mono audio (SCE + SCE) */
2379 is_dmono = ac->dmono_mode && sce_count == 2 &&2380 is_dmono = ac->dmono_mode && sce_count == 2 &&
@@ -2387,6 +2388,59 @@ static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, · 7 unmodified lines
2387 }2388 }
2388 2389
2389 return 0;2390 return 0;
2391}
2392
2393static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame,
2394 int *got_frame_ptr, GetBitContext *gb,
2395 const AVPacket *avpkt)
2396{
2397 int err;
2398 AACDecContext *ac = avctx->priv_data;
2399
2400 ac->frame = frame;
2401 *got_frame_ptr = 0;
2402
2403 if (show_bits(gb, 12) == 0xfff) {
2404 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2405 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2406 goto fail;
2407 }
2408 if (ac->oc[1].m4ac.sampling_index > 12) {
2409 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2410 err = AVERROR_INVALIDDATA;
2411 goto fail;
2412 }
2413 }
2414
2415 if ((err = frame_configure_elements(avctx)) < 0)
2416 goto fail;
2417
2418 // The AV_PROFILE_AAC_* defines are all object_type - 1
2419 // This may lead to an undefined profile being signaled
2420 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2421
2422 ac->tags_mapped = 0;
2423
2424 if ((ac->oc[1].m4ac.object_type == AOT_USAC) ||
2425 (ac->oc[1].m4ac.object_type == AOT_USAC_NOSBR)) {
2426 if (ac->is_fixed) {
2427 avpriv_report_missing_feature(ac->avctx,
2428 "AAC USAC fixed-point decoding");
2429 return AVERROR_PATCHWELCOME;
2430 }
2431#if CONFIG_AAC_DECODER
2432 err = ff_aac_usac_decode_frame(avctx, ac, gb, got_frame_ptr);
2433 if (err < 0)
2434 goto fail;
2435#endif
2436 } else {
2437 err = decode_frame_ga(avctx, ac, gb, got_frame_ptr);
2438 if (err < 0)
2439 goto fail;
2440 }
2441
2442 return err;
2443
2390fail:2444fail:
2391 pop_output_configuration(ac);2445 pop_output_configuration(ac);
2392 return err;2446 return err;
@@ -2414,7 +2468,7 @@ static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 21 unmodified lines
2414 if (new_extradata) {2468 if (new_extradata) {
2415 /* discard previous configuration */2469 /* discard previous configuration */
2416 ac->oc[1].status = OC_NONE;2470 ac->oc[1].status = OC_NONE;
2417 err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,2471 err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
2418 new_extradata,2472 new_extradata,
2419 new_extradata_size * 8LL, 1);2473 new_extradata_size * 8LL, 1);
2420 if (err < 0) {2474 if (err < 0) {
libavcodec/aac/aacdec.h+187−0
@@ -42,6 +42,8 @@ · 41 unmodified lines
42#include "libavcodec/avcodec.h"42#include "libavcodec/avcodec.h"
43#include "libavcodec/mpeg4audio.h"43#include "libavcodec/mpeg4audio.h"
44 44
45#include "aacdec_ac.h"
46
45typedef struct AACDecContext AACDecContext;47typedef struct AACDecContext AACDecContext;
46 48
47/**49/**
@@ -69,6 +71,32 @@ enum CouplingPoint { · 21 unmodified lines
69 AFTER_IMDCT = 3,71 AFTER_IMDCT = 3,
70};72};
71 73
74enum AACUsacElem {
75 ID_USAC_SCE = 0,
76 ID_USAC_CPE = 1,
77 ID_USAC_LFE = 2,
78 ID_USAC_EXT = 3,
79};
80
81enum ExtensionHeaderType {
82 ID_CONFIG_EXT_FILL = 0,
83 ID_CONFIG_EXT_LOUDNESS_INFO = 2,
84 ID_CONFIG_EXT_STREAM_ID = 7,
85};
86
87enum AACUsacExtension {
88 ID_EXT_ELE_FILL,
89 ID_EXT_ELE_MPEGS,
90 ID_EXT_ELE_SAOC,
91 ID_EXT_ELE_AUDIOPREROLL,
92 ID_EXT_ELE_UNI_DRC,
93};
94
95enum AACUSACLoudnessExt {
96 UNIDRCLOUDEXT_TERM = 0x0,
97 UNIDRCLOUDEXT_EQ = 0x1,
98};
99
72// Supposed to be equal to AAC_RENAME() in case of USE_FIXED.100// Supposed to be equal to AAC_RENAME() in case of USE_FIXED.
73#define RENAME_FIXED(name) name ## _fixed101#define RENAME_FIXED(name) name ## _fixed
74 102
@@ -93,6 +121,40 @@ typedef struct LongTermPrediction { · 18 unmodified lines
93 int8_t used[MAX_LTP_LONG_SFB];121 int8_t used[MAX_LTP_LONG_SFB];
94} LongTermPrediction;122} LongTermPrediction;
95 123
124/* Per channel core mode */
125typedef struct AACUsacElemData {
126 uint8_t core_mode;
127 uint8_t scale_factor_grouping;
128
129 /* Timewarping ratio */
130#define NUM_TW_NODES 16
131 uint8_t tw_ratio[NUM_TW_NODES];
132
133 struct {
134 uint8_t acelp_core_mode : 3;
135 uint8_t lpd_mode : 5;
136
137 uint8_t bpf_control_info : 1;
138 uint8_t core_mode_last : 1;
139 uint8_t fac_data_present : 1;
140
141 int last_lpd_mode;
142 } ldp;
143
144 struct {
145 unsigned int seed;
146 uint8_t level : 3;
147 uint8_t offset : 5;
148 } noise;
149
150 struct {
151 uint8_t gain;
152 uint32_t kv[8 /* (1024 / 16) / 8 */][8];
153 } fac;
154
155 AACArithState ac;
156} AACUsacElemData;
157
96/**158/**
97 * Individual Channel Stream159 * Individual Channel Stream
98 */160 */
@@ -145,11 +207,13 @@ typedef struct ChannelCoupling { · 46 unmodified lines
145 */207 */
146typedef struct SingleChannelElement {208typedef struct SingleChannelElement {
147 IndividualChannelStream ics;209 IndividualChannelStream ics;
210 AACUsacElemData ue; ///< USAC element data
148 TemporalNoiseShaping tns;211 TemporalNoiseShaping tns;
149 enum BandType band_type[128]; ///< band types212 enum BandType band_type[128]; ///< band types
150 int sfo[128]; ///< scalefactor offsets213 int sfo[128]; ///< scalefactor offsets
151 INTFLOAT_UNION(sf, [128]); ///< scalefactors (8 windows * 16 sfb max)214 INTFLOAT_UNION(sf, [128]); ///< scalefactors (8 windows * 16 sfb max)
152 INTFLOAT_ALIGNED_UNION(32, coeffs, 1024); ///< coefficients for IMDCT, maybe processed215 INTFLOAT_ALIGNED_UNION(32, coeffs, 1024); ///< coefficients for IMDCT, maybe processed
216 INTFLOAT_ALIGNED_UNION(32, prev_coeffs, 1024); ///< unscaled previous contents of coeffs[] for USAC
153 INTFLOAT_ALIGNED_UNION(32, saved, 1536); ///< overlap217 INTFLOAT_ALIGNED_UNION(32, saved, 1536); ///< overlap
154 INTFLOAT_ALIGNED_UNION(32, ret_buf, 2048); ///< PCM output buffer218 INTFLOAT_ALIGNED_UNION(32, ret_buf, 2048); ///< PCM output buffer
155 INTFLOAT_ALIGNED_UNION(16, ltp_state, 3072); ///< time signal for LTP219 INTFLOAT_ALIGNED_UNION(16, ltp_state, 3072); ///< time signal for LTP
@@ -163,25 +227,148 @@ typedef struct SingleChannelElement { · 7 unmodified lines
163 };227 };
164} SingleChannelElement;228} SingleChannelElement;
165 229
230typedef struct AACUsacStereo {
231 uint8_t common_window;
232 uint8_t common_tw;
233
234 uint8_t ms_mask_mode;
235 uint8_t config_idx;
236
237 /* Complex prediction */
238 uint8_t use_prev_frame;
239 uint8_t pred_dir;
240 uint8_t complex_coef;
241
242 uint8_t pred_used[128];
243
244 INTFLOAT_ALIGNED_UNION(32, alpha_q_re, 1024);
245 INTFLOAT_ALIGNED_UNION(32, alpha_q_im, 1024);
246 INTFLOAT_ALIGNED_UNION(32, prev_alpha_q_re, 1024);
247 INTFLOAT_ALIGNED_UNION(32, prev_alpha_q_im, 1024);
248
249 INTFLOAT_ALIGNED_UNION(32, dmix_re, 1024);
250 INTFLOAT_ALIGNED_UNION(32, prev_dmix_re, 1024); /* Recalculated on every frame */
251 INTFLOAT_ALIGNED_UNION(32, dmix_im, 1024); /* Final prediction data */
252} AACUsacStereo;
253
166/**254/**
167 * channel element - generic struct for SCE/CPE/CCE/LFE255 * channel element - generic struct for SCE/CPE/CCE/LFE
168 */256 */
169typedef struct ChannelElement {257typedef struct ChannelElement {
170 int present;258 int present;
171 // CPE specific259 // CPE specific
260 uint8_t max_sfb_ste; ///< (USAC) Maximum of both max_sfb values
172 uint8_t ms_mask[128]; ///< Set if mid/side stereo is used for each scalefactor window band261 uint8_t ms_mask[128]; ///< Set if mid/side stereo is used for each scalefactor window band
173 // shared262 // shared
174 SingleChannelElement ch[2];263 SingleChannelElement ch[2];
175 // CCE specific264 // CCE specific
176 ChannelCoupling coup;265 ChannelCoupling coup;
266 // USAC stereo coupling data
267 AACUsacStereo us;
177} ChannelElement;268} ChannelElement;
178 269
270typedef struct AACUSACLoudnessInfo {
271 uint8_t drc_set_id : 6;
272 uint8_t downmix_id : 7;
273 struct {
274 uint16_t lvl : 12;
275 uint8_t present : 1;
276 } sample_peak;
277
278 struct {
279 uint16_t lvl : 12;
280 uint8_t measurement : 4;
281 uint8_t reliability : 2;
282 uint8_t present : 1;
283 } true_peak;
284
285 uint8_t nb_measurements : 4;
286 struct {
287 uint8_t method_def : 4;
288 uint8_t method_val;
289 uint8_t measurement : 4;
290 uint8_t reliability : 2;
291 } measurements[16];
292} AACUSACLoudnessInfo;
293
294typedef struct AACUsacElemConfig {
295 enum AACUsacElem type;
296
297 uint8_t tw_mdct : 1;
298 uint8_t noise_fill : 1;
299
300 uint8_t stereo_config_index;
301
302 struct {
303 int ratio;
304
305 uint8_t harmonic_sbr : 1; /* harmonicSBR */
306 uint8_t bs_intertes : 1; /* bs_interTes */
307 uint8_t bs_pvc : 1; /* bs_pvc */
308
309 struct {
310 uint8_t start_freq; /* dflt_start_freq */
311 uint8_t stop_freq; /* dflt_stop_freq */
312
313 uint8_t freq_scale; /* dflt_freq_scale */
314 uint8_t alter_scale : 1; /* dflt_alter_scale */
315 uint8_t noise_scale; /* dflt_noise_scale */
316
317 uint8_t limiter_bands; /* dflt_limiter_bands */
318 uint8_t limiter_gains; /* dflt_limiter_gains */
319 uint8_t interpol_freq : 1; /* dflt_interpol_freq */
320 uint8_t smoothing_mode : 1; /* dflt_smoothing_mode */
321 } dflt;
322 } sbr;
323
324 struct {
325 uint8_t freq_res; /* bsFreqRes */
326 uint8_t fixed_gain; /* bsFixedGainDMX */
327 uint8_t temp_shape_config; /* bsTempShapeConfig */
328 uint8_t decorr_config; /* bsDecorrConfig */
329 uint8_t high_rate_mode : 1; /* bsHighRateMode */
330 uint8_t phase_coding : 1; /* bsPhaseCoding */
331
332 uint8_t otts_bands_phase; /* bsOttBandsPhase */
333 uint8_t residual_coding; /* bsResidualCoding */
334 uint8_t residual_bands; /* bsResidualBands */
335 uint8_t pseudo_lr : 1; /* bsPseudoLr */
336 uint8_t env_quant_mode : 1; /* bsEnvQuantMode */
337 } mps;
338
339 struct {
340 enum AACUsacExtension type;
341 uint8_t payload_frag;
342 uint32_t default_len;
343 uint32_t pl_data_offset;
344 uint8_t *pl_data;
345 } ext;
346} AACUsacElemConfig;
347
348typedef struct AACUSACConfig {
349 uint8_t core_sbr_frame_len_idx; /* coreSbrFrameLengthIndex */
350 uint8_t rate_idx;
351 uint16_t core_frame_len;
352 uint16_t stream_identifier;
353
354 AACUsacElemConfig elems[64];
355 int nb_elems;
356
357 struct {
358 uint8_t nb_album;
359 AACUSACLoudnessInfo album_info[64];
360 uint8_t nb_info;
361 AACUSACLoudnessInfo info[64];
362 } loudness;
363} AACUSACConfig;
364
179typedef struct OutputConfiguration {365typedef struct OutputConfiguration {
180 MPEG4AudioConfig m4ac;366 MPEG4AudioConfig m4ac;
181 uint8_t layout_map[MAX_ELEM_ID*4][3];367 uint8_t layout_map[MAX_ELEM_ID*4][3];
182 int layout_map_tags;368 int layout_map_tags;
183 AVChannelLayout ch_layout;369 AVChannelLayout ch_layout;
184 enum OCStatus status;370 enum OCStatus status;
371 AACUSACConfig usac;
185} OutputConfiguration;372} OutputConfiguration;
186 373
187/**374/**
libavcodec/aac/aacdec_ac.c+208−0
@@ -0,0 +1,208 @@
1/*
2 * AAC definitions and structures
3 * Copyright (c) 2024 Lynne
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavcodec/aactab.h"
23#include "aacdec_ac.h"
24
25uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
26{
27 float ratio;
28 if (reset) {
29 memset(state->last, 0, sizeof(state->last));
30 state->last_len = N;
31 } else if (state->last_len != N) {
32 int i;
33 uint8_t last[512 /* 2048 / 4 */];
34 memcpy(last, state->last, sizeof(last));
35
36 ratio = state->last_len / (float)N;
37 for (i = 0; i < N/2; i++) {
38 int k = (int)(i * ratio);
39 state->last[i] = last[k];
40 }
41
42 for (; i < FF_ARRAY_ELEMS(state->last); i++)
43 state->last[i] = 0;
44
45 state->last_len = N;
46 }
47
48 state->cur[3] = 0;
49 state->cur[2] = 0;
50 state->cur[1] = 0;
51 state->cur[0] = 1;
52
53 state->state_pre = state->last[0] << 12;
54 return state->last[0] << 12;
55}
56
57uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
58{
59 c = state->state_pre >> 8;
60 c = c + (state->last[i + 1] << 8);
61 c = (c << 4);
62 c += state->cur[1];
63
64 state->state_pre = c;
65
66 if (i > 3 &&
67 ((state->cur[3] + state->cur[2] + state->cur[1]) < 5))
68 return c + 0x10000;
69
70 return c;
71}
72
73uint32_t ff_aac_ac_get_pk(uint32_t c)
74{
75 int i_min = -1;
76 int i, j;
77 int i_max = FF_ARRAY_ELEMS(ff_aac_ac_lookup_m) - 1;
78 while ((i_max - i_min) > 1) {
79 i = i_min + ((i_max - i_min) / 2);
80 j = ff_aac_ac_hash_m[i];
81 if (c < (j >> 8))
82 i_max = i;
83 else if (c > (j >> 8))
84 i_min = i;
85 else
86 return (j & 0xFF);
87 }
88 return ff_aac_ac_lookup_m[i_max];
89}
90
91void ff_aac_ac_update_context(AACArithState *state, int idx,
92 uint16_t a, uint16_t b)
93{
94 state->cur[0] = a + b + 1;
95 if (state->cur[0] > 0xF)
96 state->cur[0] = 0xF;
97
98 state->cur[3] = state->cur[2];
99 state->cur[2] = state->cur[1];
100 state->cur[1] = state->cur[0];
101
102 state->last[idx] = state->cur[0];
103}
104
105/* Initialize AC */
106void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
107{
108 ac->low = 0;
109 ac->high = UINT16_MAX;
110 ac->val = get_bits(gb, 16);
111}
112
113uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb,
114 const uint16_t *cdf, uint16_t cdf_len)
115{
116 int val = ac->val;
117 int low = ac->low;
118 int high = ac->high;
119
120 int sym;
121 int rng = high - low + 1;
122 int c = ((((int)(val - low + 1)) << 14) - ((int)1));
123
124 const uint16_t *p = cdf - 1;
125
126 /* One for each possible CDF length in the spec */
127 switch (cdf_len) {
128 case 2:
129 if ((p[1] * rng) > c)
130 p += 1;
131 break;
132 case 4:
133 if ((p[2] * rng) > c)
134 p += 2;
135 if ((p[1] * rng) > c)
136 p += 1;
137 break;
138 case 17:
139 /* First check if the current probability is even met at all */
140 if ((p[1] * rng) <= c)
141 break;
142 p += 1;
143 for (int i = 8; i >= 1; i >>= 1)
144 if ((p[i] * rng) > c)
145 p += i;
146 break;
147 case 27:
148 if ((p[16] * rng) > c)
149 p += 16;
150 if ((p[8] * rng) > c)
151 p += 8;
152 if (p != (cdf - 1 + 24))
153 if ((p[4] * rng) > c)
154 p += 4;
155 if ((p[2] * rng) > c)
156 p += 2;
157
158 if (p != (cdf - 1 + 24 + 2))
159 if ((p[1] * rng) > c)
160 p += 1;
161 break;
162 default:
163 /* This should never happen */
164 av_assert2(0);
165 }
166
167 sym = (int)((ptrdiff_t)(p - cdf)) + 1;
168 if (sym)
169 high = low + ((rng * cdf[sym - 1]) >> 14) - 1;
170 low += (rng * cdf[sym]) >> 14;
171
172 /* This loop could be done faster */
173 while (1) {
174 if (high < 32768) {
175 ;
176 } else if (low >= 32768) {
177 val -= 32768;
178 low -= 32768;
179 high -= 32768;
180 } else if (low >= 16384 && high < 49152) {
181 val -= 16384;
182 low -= 16384;
183 high -= 16384;
184 } else {
185 break;
186 }
187 low += low;
188 high += high + 1;
189 val = (val << 1) | get_bits1(gb);
190 };
191
192 ac->low = low;
193 ac->high = high;
194 ac->val = val;
195
196 return sym;
197}
198
199void ff_aac_ac_finish(AACArithState *state, int offset, int N)
200{
201 int i;
202
203 for (i = offset; i < N/2; i++)
204 state->last[i] = 1;
205
206 for (; i < FF_ARRAY_ELEMS(state->last); i++)
207 state->last[i] = 0;
208}
libavcodec/aac/aacdec_ac.h+54−0
@@ -0,0 +1,54 @@
1/*
2 * AAC definitions and structures
3 * Copyright (c) 2024 Lynne
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef AVCODEC_AAC_AACDEC_AC_H
23#define AVCODEC_AAC_AACDEC_AC_H
24
25#include "libavcodec/get_bits.h"
26
27typedef struct AACArithState {
28 uint8_t last[512 /* 2048 / 4 */];
29 int last_len;
30 uint8_t cur[4];
31 uint16_t state_pre;
32} AACArithState;
33
34typedef struct AACArith {
35 uint16_t low;
36 uint16_t high;
37 uint16_t val;
38} AACArith;
39
40#define FF_AAC_AC_ESCAPE 16
41
42uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int len);
43uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t old_c, int idx, int len);
44uint32_t ff_aac_ac_get_pk(uint32_t c);
45
46void ff_aac_ac_update_context(AACArithState *state, int idx, uint16_t a, uint16_t b);
47void ff_aac_ac_init(AACArith *ac, GetBitContext *gb);
48
49uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb,
50 const uint16_t *cdf, uint16_t cdf_len);
51
52void ff_aac_ac_finish(AACArithState *state, int offset, int nb);
53
54#endif /* AVCODEC_AACDEC_AC_H */
libavcodec/aac/aacdec_dsp_template.c+2−2
@@ -88,8 +88,8 @@ static void AAC_RENAME(apply_mid_side_stereo)(AACDecContext *ac, ChannelElement · 87 unmodified lines
88 INTFLOAT *ch1 = cpe->ch[1].AAC_RENAME(coeffs);88 INTFLOAT *ch1 = cpe->ch[1].AAC_RENAME(coeffs);
89 const uint16_t *offsets = ics->swb_offset;89 const uint16_t *offsets = ics->swb_offset;
90 for (int g = 0; g < ics->num_window_groups; g++) {90 for (int g = 0; g < ics->num_window_groups; g++) {
91 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {91 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
92 const int idx = g*ics->max_sfb + sfb;92 const int idx = g*cpe->max_sfb_ste + sfb;
93 if (cpe->ms_mask[idx] &&93 if (cpe->ms_mask[idx] &&
94 cpe->ch[0].band_type[idx] < NOISE_BT &&94 cpe->ch[0].band_type[idx] < NOISE_BT &&
95 cpe->ch[1].band_type[idx] < NOISE_BT) {95 cpe->ch[1].band_type[idx] < NOISE_BT) {
libavcodec/aac/aacdec_latm.h+8−6
@@ -56,7 +56,8 @@ static int latm_decode_audio_specific_config(struct LATMContext *latmctx, · 55 unmodified lines
56{56{
57 AACDecContext *ac = &latmctx->aac_ctx;57 AACDecContext *ac = &latmctx->aac_ctx;
58 AVCodecContext *avctx = ac->avctx;58 AVCodecContext *avctx = ac->avctx;
59 MPEG4AudioConfig m4ac = { 0 };59 OutputConfiguration oc = { 0 };
60 MPEG4AudioConfig *m4ac = &oc.m4ac;
60 GetBitContext gbc;61 GetBitContext gbc;
61 int config_start_bit = get_bits_count(gb);62 int config_start_bit = get_bits_count(gb);
62 int sync_extension = 0;63 int sync_extension = 0;
@@ -76,7 +77,7 @@ static int latm_decode_audio_specific_config(struct LATMContext *latmctx, · 13 unmodified lines
76 if (get_bits_left(gb) <= 0)77 if (get_bits_left(gb) <= 0)
77 return AVERROR_INVALIDDATA;78 return AVERROR_INVALIDDATA;
78 79
79 bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &m4ac,80 bits_consumed = decode_audio_specific_config_gb(NULL, avctx, &oc,
80 &gbc, config_start_bit,81 &gbc, config_start_bit,
81 sync_extension);82 sync_extension);
82 83
@@ -88,11 +89,12 @@ static int latm_decode_audio_specific_config(struct LATMContext *latmctx, · 5 unmodified lines
88 asclen = bits_consumed;89 asclen = bits_consumed;
89 90
90 if (!latmctx->initialized ||91 if (!latmctx->initialized ||
91 ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||92 ac->oc[1].m4ac.sample_rate != m4ac->sample_rate ||
92 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {93 ac->oc[1].m4ac.chan_config != m4ac->chan_config) {
93 94
94 if (latmctx->initialized) {95 if (latmctx->initialized) {
95 av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n", m4ac.sample_rate, m4ac.chan_config);96 av_log(avctx, AV_LOG_INFO, "audio config changed (sample_rate=%d, chan_config=%d)\n",
97 m4ac->sample_rate, m4ac->chan_config);
96 } else {98 } else {
97 av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");99 av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
98 }100 }
@@ -280,7 +282,7 @@ static int latm_decode_frame(AVCodecContext *avctx, AVFrame *out, · 181 unmodified lines
280 } else {282 } else {
281 push_output_configuration(&latmctx->aac_ctx);283 push_output_configuration(&latmctx->aac_ctx);
282 if ((err = decode_audio_specific_config(284 if ((err = decode_audio_specific_config(
283 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,285 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1],
284 avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {286 avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
285 pop_output_configuration(&latmctx->aac_ctx);287 pop_output_configuration(&latmctx->aac_ctx);
286 return err;288 return err;
libavcodec/aac/aacdec_lpd.c+198−0
@@ -0,0 +1,198 @@
1/*
2 * Copyright (c) 2024 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "aacdec_lpd.h"
22#include "aacdec_usac.h"
23#include "libavcodec/unary.h"
24
25const uint8_t ff_aac_lpd_mode_tab[32][4] = {
26 { 0, 0, 0, 0 },
27 { 1, 0, 0, 0 },
28 { 0, 1, 0, 0 },
29 { 1, 1, 0, 0 },
30 { 0, 0, 1, 0 },
31 { 1, 0, 1, 0 },
32 { 0, 1, 1, 0 },
33 { 1, 1, 1, 0 },
34 { 0, 0, 0, 1 },
35 { 1, 0, 0, 1 },
36 { 0, 1, 0, 1 },
37 { 1, 1, 0, 1 },
38 { 0, 0, 1, 1 },
39 { 1, 0, 1, 1 },
40 { 0, 1, 1, 1 },
41 { 1, 1, 1, 1 },
42 { 2, 2, 0, 0 },
43 { 2, 2, 1, 0 },
44 { 2, 2, 0, 1 },
45 { 2, 2, 1, 1 },
46 { 0, 0, 2, 2 },
47 { 1, 0, 2, 2 },
48 { 0, 1, 2, 2 },
49 { 1, 1, 2, 2 },
50 { 2, 2, 2, 2 },
51 { 3, 3, 3, 3 },
52 /* Larger values are reserved, but permit them for resilience */
53 { 0, 0, 0, 0 },
54 { 0, 0, 0, 0 },
55 { 0, 0, 0, 0 },
56 { 0, 0, 0, 0 },
57 { 0, 0, 0, 0 },
58 { 0, 0, 0, 0 },
59};
60
61static void parse_qn(GetBitContext *gb, int *qn, int nk_mode, int no_qn)
62{
63 if (nk_mode == 1) {
64 for (int k = 0; k < no_qn; k++) {
65 qn[k] = get_unary(gb, 0, INT32_MAX); // TODO: find proper ranges
66 if (qn[k])
67 qn[k]++;
68 }
69 return;
70 }
71
72 for (int k = 0; k < no_qn; k++)
73 qn[k] = get_bits(gb, 2) + 2;
74
75 if (nk_mode == 2) {
76 for (int k = 0; k < no_qn; k++) {
77 if (qn[k] > 4) {
78 qn[k] = get_unary(gb, 0, INT32_MAX);;
79 if (qn[k])
80 qn[k] += 4;
81 }
82 }
83 return;
84 }
85
86 for (int k = 0; k < no_qn; k++) {
87 if (qn[k] > 4) {
88 int qn_ext = get_unary(gb, 0, INT32_MAX);;
89 switch (qn_ext) {
90 case 0: qn[k] = 5; break;
91 case 1: qn[k] = 6; break;
92 case 2: qn[k] = 0; break;
93 default: qn[k] = qn_ext + 4; break;
94 }
95 }
96 }
97}
98
99static int parse_codebook_idx(GetBitContext *gb, uint32_t *kv,
100 int nk_mode, int no_qn)
101{
102 int idx, n, nk;
103
104 int qn[2];
105 parse_qn(gb, qn, nk_mode, no_qn);
106
107 for (int k = 0; k < no_qn; k++) {
108 if (qn[k] > 4) {
109 nk = (qn[k] - 3) / 2;
110 n = qn[k] - nk*2;
111 } else {
112 nk = 0;
113 n = qn[k];
114 }
115 }
116
117 idx = get_bits(gb, 4*n);
118
119 if (nk > 0)
120 for (int i = 0; i < 8; i++)
121 kv[i] = get_bits(gb, nk);
122
123 return 0;
124}
125
126int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb,
127 int use_gain, int len)
128{
129 int ret;
130 if (use_gain)
131 ce->fac.gain = get_bits(gb, 7);
132
133 for (int i = 0; i < len/8; i++) {
134 ret = parse_codebook_idx(gb, ce->fac.kv[i], 1, 1);
135 if (ret < 0)
136 return ret;
137 }
138
139 return 0;
140}
141
142int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac,
143 AACUsacElemData *ce, GetBitContext *gb)
144{
145 int k;
146 const uint8_t *mod;
147 int first_ldp_flag;
148 int first_tcx_flag;
149
150 ce->ldp.acelp_core_mode = get_bits(gb, 3);
151 ce->ldp.lpd_mode = get_bits(gb, 5);
152
153 ce->ldp.bpf_control_info = get_bits1(gb);
154 ce->ldp.core_mode_last = get_bits1(gb);
155 ce->ldp.fac_data_present = get_bits1(gb);
156
157 mod = ff_aac_lpd_mode_tab[ce->ldp.lpd_mode];
158
159 first_ldp_flag = !ce->ldp.core_mode_last;
160 first_tcx_flag = 1;
161 if (first_ldp_flag)
162 ce->ldp.last_lpd_mode = -1; /* last_ldp_mode is a **STATEFUL** value */
163
164 k = 0;
165 while (k < 0) {
166 if (!k) {
167 if (ce->ldp.core_mode_last && ce->ldp.fac_data_present)
168 ff_aac_parse_fac_data(ce, gb, 0, usac->core_frame_len/8);
169 } else {
170 if (!ce->ldp.last_lpd_mode && mod[k] > 0 ||
171 ce->ldp.last_lpd_mode && !mod[k])
172 ff_aac_parse_fac_data(ce, gb, 0, usac->core_frame_len/8);
173 }
174 if (!mod[k]) {
175// parse_acelp_coding();
176 ce->ldp.last_lpd_mode = 0;
177 k++;
178 } else {
179// parse_tcx_coding();
180 ce->ldp.last_lpd_mode = mod[k];
181 k += (1 << (mod[k] - 1));
182 first_tcx_flag = 0;
183 }
184 }
185
186// parse_lpc_data(first_lpd_flag);
187
188 if (!ce->ldp.core_mode_last && ce->ldp.fac_data_present) {
189 uint16_t len_8 = usac->core_frame_len / 8;
190 uint16_t len_16 = usac->core_frame_len / 16;
191 uint16_t fac_len = get_bits1(gb) /* short_fac_flag */ ? len_8 : len_16;
192 int ret = ff_aac_parse_fac_data(ce, gb, 1, fac_len);
193 if (ret < 0)
194 return ret;
195 }
196
197 return 0;
198}
libavcodec/aac/aacdec_lpd.h+33−0
@@ -0,0 +1,33 @@
1/*
2 * Copyright (c) 2024 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVCODEC_AAC_AACDEC_LPD_H
22#define AVCODEC_AAC_AACDEC_LPD_H
23
24#include "aacdec.h"
25#include "libavcodec/get_bits.h"
26
27int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb,
28 int use_gain, int len);
29
30int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac,
31 AACUsacElemData *ce, GetBitContext *gb);
32
33#endif /* AVCODEC_AAC_AACDEC_LPD_H */
libavcodec/aac/aacdec_usac.c+1608−0
@@ -0,0 +1,1608 @@
1/*
2 * Copyright (c) 2024 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "aacdec_usac.h"
22#include "aacdec_tab.h"
23#include "aacdec_lpd.h"
24#include "aacdec_ac.h"
25
26#include "libavcodec/aactab.h"
27#include "libavutil/mem.h"
28#include "libavcodec/mpeg4audio.h"
29#include "libavcodec/unary.h"
30
31/* Number of scalefactor bands per complex prediction band, equal to 2. */
32#define SFB_PER_PRED_BAND 2
33
34static inline uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3)
35{
36 uint32_t val = get_bits(gb, nb1), val2;
37 if (val < ((1 << nb1) - 1))
38 return val;
39
40 val += val2 = get_bits(gb, nb2);
41 if (val2 == ((1 << nb2) - 1))
42 val += get_bits(gb, nb3);
43
44 return val;
45}
46
47/* ISO/IEC 23003-3, Table 74 — bsOutputChannelPos */
48static const enum AVChannel usac_ch_pos_to_av[64] = {
49 [0] = AV_CHAN_FRONT_LEFT,
50 [1] = AV_CHAN_FRONT_RIGHT,
51 [2] = AV_CHAN_FRONT_CENTER,
52 [3] = AV_CHAN_LOW_FREQUENCY,
53 [4] = AV_CHAN_SIDE_LEFT, // +110 degrees, Ls|LS|kAudioChannelLabel_LeftSurround
54 [5] = AV_CHAN_SIDE_RIGHT, // -110 degrees, Rs|RS|kAudioChannelLabel_RightSurround
55 [6] = AV_CHAN_FRONT_LEFT_OF_CENTER,
56 [7] = AV_CHAN_FRONT_RIGHT_OF_CENTER,
57 [8] = AV_CHAN_BACK_LEFT, // +135 degrees, Lsr|BL|kAudioChannelLabel_RearSurroundLeft
58 [9] = AV_CHAN_BACK_RIGHT, // -135 degrees, Rsr|BR|kAudioChannelLabel_RearSurroundRight
59 [10] = AV_CHAN_BACK_CENTER,
60 [11] = AV_CHAN_SURROUND_DIRECT_LEFT,
61 [12] = AV_CHAN_SURROUND_DIRECT_RIGHT,
62 [13] = AV_CHAN_SIDE_SURROUND_LEFT, // +90 degrees, Lss|SL|kAudioChannelLabel_LeftSideSurround
63 [14] = AV_CHAN_SIDE_SURROUND_RIGHT, // -90 degrees, Rss|SR|kAudioChannelLabel_RightSideSurround
64 [15] = AV_CHAN_WIDE_LEFT, // +60 degrees, Lw|FLw|kAudioChannelLabel_LeftWide
65 [16] = AV_CHAN_WIDE_RIGHT, // -60 degrees, Rw|FRw|kAudioChannelLabel_RightWide
66 [17] = AV_CHAN_TOP_FRONT_LEFT,
67 [18] = AV_CHAN_TOP_FRONT_RIGHT,
68 [19] = AV_CHAN_TOP_FRONT_CENTER,
69 [20] = AV_CHAN_TOP_BACK_LEFT,
70 [21] = AV_CHAN_TOP_BACK_RIGHT,
71 [22] = AV_CHAN_TOP_BACK_CENTER,
72 [23] = AV_CHAN_TOP_SIDE_LEFT,
73 [24] = AV_CHAN_TOP_SIDE_RIGHT,
74 [25] = AV_CHAN_TOP_CENTER,
75 [26] = AV_CHAN_LOW_FREQUENCY_2,
76 [27] = AV_CHAN_BOTTOM_FRONT_LEFT,
77 [28] = AV_CHAN_BOTTOM_FRONT_RIGHT,
78 [29] = AV_CHAN_BOTTOM_FRONT_CENTER,
79 [30] = AV_CHAN_TOP_SURROUND_LEFT, ///< +110 degrees, Lvs, TpLS
80 [31] = AV_CHAN_TOP_SURROUND_RIGHT, ///< -110 degrees, Rvs, TpRS
81};
82
83static int decode_loudness_info(AACDecContext *ac, AACUSACLoudnessInfo *info,
84 GetBitContext *gb)
85{
86 info->drc_set_id = get_bits(gb, 6);
87 info->downmix_id = get_bits(gb, 7);
88
89 if ((info->sample_peak.present = get_bits1(gb))) /* samplePeakLevelPresent */
90 info->sample_peak.lvl = get_bits(gb, 12);
91
92 if ((info->true_peak.present = get_bits1(gb))) { /* truePeakLevelPresent */
93 info->true_peak.lvl = get_bits(gb, 12);
94 info->true_peak.measurement = get_bits(gb, 4);
95 info->true_peak.reliability = get_bits(gb, 2);
96 }
97
98 info->nb_measurements = get_bits(gb, 4);
99 for (int i = 0; i < info->nb_measurements; i++) {
100 info->measurements[i].method_def = get_bits(gb, 4);
101 info->measurements[i].method_val = get_unary(gb, 0, 8);
102 info->measurements[i].measurement = get_bits(gb, 4);
103 info->measurements[i].reliability = get_bits(gb, 2);
104 }
105
106 return 0;
107}
108
109static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac,
110 GetBitContext *gb)
111{
112 int ret;
113
114 usac->loudness.nb_album = get_bits(gb, 6); /* loudnessInfoAlbumCount */
115 usac->loudness.nb_info = get_bits(gb, 6); /* loudnessInfoCount */
116
117 for (int i = 0; i < usac->loudness.nb_album; i++) {
118 ret = decode_loudness_info(ac, &usac->loudness.album_info[i], gb);
119 if (ret < 0)
120 return ret;
121 }
122
123 for (int i = 0; i < usac->loudness.nb_info; i++) {
124 ret = decode_loudness_info(ac, &usac->loudness.info[i], gb);
125 if (ret < 0)
126 return ret;
127 }
128
129 if (get_bits1(gb)) { /* loudnessInfoSetExtPresent */
130 enum AACUSACLoudnessExt type;
131 while ((type = get_bits(gb, 4)) != UNIDRCLOUDEXT_TERM) {
132 uint8_t size_bits = get_bits(gb, 4) + 4;
133 uint8_t bit_size = get_bits(gb, size_bits) + 1;
134 switch (type) {
135 case UNIDRCLOUDEXT_EQ:
136 avpriv_report_missing_feature(ac->avctx, "loudnessInfoV1");
137 return AVERROR_PATCHWELCOME;
138 default:
139 for (int i = 0; i < bit_size; i++)
140 skip_bits1(gb);
141 }
142 }
143 }
144
145 return 0;
146}
147
148static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb)
149{
150 uint8_t header_extra1;
151 uint8_t header_extra2;
152
153 e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
154 e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
155 e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
156
157 e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
158 e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
159
160 header_extra1 = get_bits1(gb); /* dflt_header_extra1 */
161 header_extra2 = get_bits1(gb); /* dflt_header_extra2 */
162
163 e->sbr.dflt.freq_scale = 2;
164 e->sbr.dflt.alter_scale = 1;
165 e->sbr.dflt.noise_scale = 2;
166 if (header_extra1) {
167 e->sbr.dflt.freq_scale = get_bits(gb, 2); /* dflt_freq_scale */
168 e->sbr.dflt.alter_scale = get_bits1(gb); /* dflt_alter_scale */
169 e->sbr.dflt.noise_scale = get_bits(gb, 2); /* dflt_noise_scale */
170 }
171
172 e->sbr.dflt.limiter_bands = 2;
173 e->sbr.dflt.limiter_gains = 2;
174 e->sbr.dflt.interpol_freq = 1;
175 e->sbr.dflt.smoothing_mode = 1;
176 if (header_extra2) {
177 e->sbr.dflt.limiter_bands = get_bits(gb, 2); /* dflt_limiter_bands */
178 e->sbr.dflt.limiter_gains = get_bits(gb, 2); /* dflt_limiter_gains */
179 e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */
180 e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */
181 }
182}
183
184static void decode_usac_element_core(AACUsacElemConfig *e,
185 GetBitContext *gb,
186 int sbr_ratio)
187{
188 e->tw_mdct = get_bits1(gb); /* tw_mdct */
189 e->noise_fill = get_bits1(gb);
190 e->sbr.ratio = sbr_ratio;
191}
192
193static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb)
194{
195 e->stereo_config_index = 0;
196 if (e->sbr.ratio) {
197 decode_usac_sbr_data(e, gb);
198 e->stereo_config_index = get_bits(gb, 2);
199 }
200 if (e->stereo_config_index) {
201 e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
202 e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
203 e->mps.temp_shape_config = get_bits(gb, 2); /* bsTempShapeConfig */
204 e->mps.decorr_config = get_bits(gb, 2); /* bsDecorrConfig */
205 e->mps.high_rate_mode = get_bits1(gb); /* bsHighRateMode */
206 e->mps.phase_coding = get_bits1(gb); /* bsPhaseCoding */
207
208 if (get_bits1(gb)) /* bsOttBandsPhasePresent */
209 e->mps.otts_bands_phase = get_bits(gb, 5); /* bsOttBandsPhase */
210
211 e->mps.residual_coding = e->stereo_config_index >= 2; /* bsResidualCoding */
212 if (e->mps.residual_coding) {
213 e->mps.residual_bands = get_bits(gb, 5); /* bsResidualBands */
214 e->mps.pseudo_lr = get_bits1(gb); /* bsPseudoLr */
215 }
216 if (e->mps.temp_shape_config == 2)
217 e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
218 }
219}
220
221static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e,
222 GetBitContext *gb)
223{
224 int len = 0, ext_config_len;
225
226 e->ext.type = get_escaped_value(gb, 4, 8, 16); /* usacExtElementType */
227 ext_config_len = get_escaped_value(gb, 4, 8, 16); /* usacExtElementConfigLength */
228
229 if (get_bits1(gb)) /* usacExtElementDefaultLengthPresent */
230 len = get_escaped_value(gb, 8, 16, 0) + 1;
231
232 e->ext.default_len = len;
233 e->ext.payload_frag = get_bits1(gb); /* usacExtElementPayloadFrag */
234
235 av_log(ac->avctx, AV_LOG_DEBUG, "Extension present: type %i, len %i\n",
236 e->ext.type, ext_config_len);
237
238 switch (e->ext.type) {
239#if 0 /* Skip unsupported values */
240 case ID_EXT_ELE_MPEGS:
241 break;
242 case ID_EXT_ELE_SAOC:
243 break;
244 case ID_EXT_ELE_UNI_DRC:
245 break;
246#endif
247 case ID_EXT_ELE_FILL:
248 break; /* This is what the spec does */
249 case ID_EXT_ELE_AUDIOPREROLL:
250 /* No configuration needed - fallthrough (len should be 0) */
251 default:
252 skip_bits(gb, 8*ext_config_len);
253 break;
254 };
255
256 return 0;
257}
258
259int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
260{
261 AACUSACConfig *usac = &oc->usac;
262 int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
263
264 ChannelElement *che;
265 enum RawDataBlockType type;
266 int id, ch;
267
268 /* Initialize state */
269 for (int i = 0; i < usac->nb_elems; i++) {
270 AACUsacElemConfig *e = &usac->elems[i];
271 if (e->type != ID_USAC_SCE && e->type != ID_USAC_CPE)
272 continue;
273
274 if (e->type == ID_USAC_SCE) {
275 ch = 1;
276 type = TYPE_SCE;
277 id = elem_id[0]++;
278 } else {
279 ch = 2;
280 type = TYPE_CPE;
281 id = elem_id[1]++;
282 }
283
284 che = ff_aac_get_che(ac, type, id);
285 if (che) {
286 AACUsacStereo *us = &che->us;
287 memset(us, 0, sizeof(*us));
288
289 for (int j = 0; j < ch; j++) {
290 SingleChannelElement *sce = &che->ch[ch];
291 AACUsacElemData *ue = &sce->ue;
292
293 memset(ue, 0, sizeof(*ue));
294
295 if (!ch)
296 ue->noise.seed = 0x3039;
297 else
298 che->ch[1].ue.noise.seed = 0x10932;
299 }
300 }
301 }
302
303 return 0;
304}
305
306/* UsacConfig */
307int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
308 GetBitContext *gb, OutputConfiguration *oc,
309 int channel_config)
310{
311 int ret, idx;
312 uint8_t freq_idx;
313 uint8_t channel_config_idx;
314 int nb_elements;
315 int samplerate;
316 int sbr_ratio;
317 MPEG4AudioConfig *m4ac = &oc->m4ac;
318 AACUSACConfig *usac = &oc->usac;
319 int elem_id[3 /* SCE, CPE, LFE */];
320
321 uint8_t layout_map[MAX_ELEM_ID*4][3];
322
323 memset(usac, 0, sizeof(*usac));
324
325 freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */
326 if (freq_idx == 0x1f) {
327 samplerate = get_bits(gb, 24); /* usacSamplingFrequency */
328
329 /* Try to match up an index for the custom sample rate.
330 * TODO: not sure if correct */
331 for (idx = 0; idx < /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32; idx++) {
332 if (ff_aac_usac_samplerate[idx] >= samplerate)
333 break;
334 }
335 idx = FFMIN(idx, /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32 - 1);
336 usac->rate_idx = idx;
337 } else {
338 samplerate = ff_aac_usac_samplerate[freq_idx];
339 if (samplerate < 0)
340 return AVERROR(EINVAL);
341 usac->rate_idx = freq_idx;
342 }
343
344 m4ac->sample_rate = avctx->sample_rate = samplerate;
345
346 usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
347 m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
348 usac->core_sbr_frame_len_idx == 2;
349
350 usac->core_frame_len = (usac->core_sbr_frame_len_idx == 0 ||
351 usac->core_sbr_frame_len_idx == 2) ? 768 : 1024;
352
353 sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
354 usac->core_sbr_frame_len_idx == 3 ? 3 :
355 usac->core_sbr_frame_len_idx == 4 ? 1 :
356 0;
357
358 channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
359 if (!channel_config_idx) {
360 /* UsacChannelConfig() */
361 uint8_t nb_channels = get_escaped_value(gb, 5, 8, 16); /* numOutChannels */
362 if (nb_channels >= 64)
363 return AVERROR(EINVAL);
364
365 av_channel_layout_uninit(&ac->oc[1].ch_layout);
366
367 ret = av_channel_layout_custom_init(&ac->oc[1].ch_layout, nb_channels);
368 if (ret < 0)
369 return ret;
370
371 for (int i = 0; i < nb_channels; i++) {
372 AVChannelCustom *cm = &ac->oc[1].ch_layout.u.map[i];
373 cm->id = usac_ch_pos_to_av[get_bits(gb, 5)]; /* bsOutputChannelPos */
374 if (cm->id == AV_CHAN_NONE)
375 cm->id = AV_CHAN_UNKNOWN;
376 }
377
378 ret = av_channel_layout_retype(&ac->oc[1].ch_layout,
379 AV_CHANNEL_ORDER_NATIVE,
380 AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
381 if (ret < 0)
382 return ret;
383
384 ret = av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
385 if (ret < 0)
386 return ret;
387 } else {
388 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
389 &nb_elements, channel_config_idx)))
390 return ret;
391 }
392
393 /* UsacDecoderConfig */
394 elem_id[0] = elem_id[1] = elem_id[2] = 0;
395 usac->nb_elems = get_escaped_value(gb, 4, 8, 16) + 1;
396
397 for (int i = 0; i < usac->nb_elems; i++) {
398 AACUsacElemConfig *e = &usac->elems[i];
399 memset(e, 0, sizeof(*e));
400
401 e->type = get_bits(gb, 2); /* usacElementType */
402 av_log(ac->avctx, AV_LOG_DEBUG, "Element present: idx %i, type %i\n",
403 i, e->type);
404
405 switch (e->type) {
406 case ID_USAC_SCE: /* SCE */
407 /* UsacCoreConfig */
408 decode_usac_element_core(e, gb, sbr_ratio);
409 if (e->sbr.ratio > 0)
410 decode_usac_sbr_data(e, gb);
411 layout_map[i][0] = TYPE_SCE;
412 layout_map[i][1] = i;
413 layout_map[i][2] = AAC_CHANNEL_FRONT;
414 elem_id[0]++;
415
416 break;
417 case ID_USAC_CPE: /* UsacChannelPairElementConf */
418 /* UsacCoreConfig */
419 decode_usac_element_core(e, gb, sbr_ratio);
420 decode_usac_element_pair(e, gb);
421 layout_map[i][0] = TYPE_CPE;
422 layout_map[i][1] = i;
423 layout_map[i][2] = AAC_CHANNEL_FRONT;
424 elem_id[1]++;
425
426 break;
427 case ID_USAC_LFE: /* LFE */
428 /* LFE has no need for any configuration */
429 e->tw_mdct = 0;
430 e->noise_fill = 0;
431 elem_id[2]++;
432 break;
433 case ID_USAC_EXT: /* EXT */
434 ret = decode_usac_extension(ac, e, gb);
435 if (ret < 0)
436 return ret;
437 break;
438 };
439 }
440
441 ret = ff_aac_output_configure(ac, layout_map, elem_id[0] + elem_id[1] + elem_id[2], OC_GLOBAL_HDR, 0);
442 if (ret < 0) {
443 av_log(avctx, AV_LOG_ERROR, "Unable to parse channel config!\n");
444 return ret;
445 }
446
447 if (get_bits1(gb)) { /* usacConfigExtensionPresent */
448 int invalid;
449 int nb_extensions = get_escaped_value(gb, 2, 4, 8) + 1; /* numConfigExtensions */
450 for (int i = 0; i < nb_extensions; i++) {
451 int type = get_escaped_value(gb, 4, 8, 16);
452 int len = get_escaped_value(gb, 4, 8, 16);
453 switch (type) {
454 case ID_CONFIG_EXT_LOUDNESS_INFO:
455 ret = decode_loudness_set(ac, usac, gb);
456 if (ret < 0)
457 return ret;
458 break;
459 case ID_CONFIG_EXT_STREAM_ID:
460 usac->stream_identifier = get_bits(gb, 16);
461 break;
462 case ID_CONFIG_EXT_FILL: /* fallthrough */
463 invalid = 0;
464 while (len--) {
465 if (get_bits(gb, 8) != 0xA5)
466 invalid++;
467 }
468 if (invalid)
469 av_log(avctx, AV_LOG_WARNING, "Invalid fill bytes: %i\n",
470 invalid);
471 break;
472 default:
473 while (len--)
474 skip_bits(gb, 8);
475 break;
476 }
477 }
478 }
479
480 ret = ff_aac_usac_reset_state(ac, oc);
481 if (ret < 0)
482 return ret;
483
484 return 0;
485}
486
487static int decode_usac_scale_factors(AACDecContext *ac,
488 SingleChannelElement *sce,
489 GetBitContext *gb, uint8_t global_gain)
490{
491 IndividualChannelStream *ics = &sce->ics;
492
493 /* Decode all scalefactors. */
494 int offset_sf = global_gain;
495 for (int g = 0; g < ics->num_window_groups; g++) {
496 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
497 /* First coefficient is just the global gain */
498 if (!g && !sfb) {
499 /* The cannonical representation of quantized scalefactors
500 * in the spec is with 100 subtracted. */
501 sce->sfo[0] = offset_sf - 100;
502 continue;
503 }
504
505 offset_sf += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
506 if (offset_sf > 255U) {
507 av_log(ac->avctx, AV_LOG_ERROR,
508 "Scalefactor (%d) out of range.\n", offset_sf);
509 return AVERROR_INVALIDDATA;
510 }
511
512 sce->sfo[g*ics->max_sfb + sfb] = offset_sf - 100;
513 }
514 }
515
516 return 0;
517}
518
519/**
520 * Decode and dequantize arithmetically coded, uniformly quantized value
521 *
522 * @param coef array of dequantized, scaled spectral data
523 * @param sf array of scalefactors or intensity stereo positions
524 *
525 * @return Returns error status. 0 - OK, !0 - error
526 */
527static int decode_spectrum_and_dequant_ac(AACDecContext *s, float coef[1024],
528 GetBitContext *gb, const float sf[120],
529 AACArithState *state, int reset,
530 uint16_t len, uint16_t N)
531{
532 AACArith ac;
533 int i, a, b;
534 uint32_t c;
535
536 int gb_count;
537 GetBitContext gb2;
538
539 ff_aac_ac_init(&ac, gb);
540 c = ff_aac_ac_map_process(state, reset, N);
541
542 /* Backup reader for rolling back by 14 bits at the end */
543 gb2 = (GetBitContext)*gb;
544 gb_count = get_bits_count(&gb2);
545
546 for (i = 0; i < len/2; i++) {
547 /* MSB */
548 int lvl, esc_nb, m;
549 c = ff_aac_ac_get_context(state, c, i, N);
550 for (lvl=esc_nb=0;;) {
551 uint32_t pki = ff_aac_ac_get_pk(c + (esc_nb << 17));
552 m = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_msb_cdfs[pki],
553 FF_ARRAY_ELEMS(ff_aac_ac_msb_cdfs[pki]));
554 if (m < FF_AAC_AC_ESCAPE)
555 break;
556 lvl++;
557
558 /* Cargo-culted value. */
559 if (lvl > 23)
560 return AVERROR(EINVAL);
561
562 if ((esc_nb = lvl) > 7)
563 esc_nb = 7;
564 }
565
566 b = m >> 2;
567 a = m - (b << 2);
568
569 /* ARITH_STOP detection */
570 if (!m) {
571 if (esc_nb)
572 break;
573 a = b = 0;
574 }
575
576 /* LSB */
577 for (int l = lvl; l > 0; l--) {
578 int lsbidx = !a ? 1 : (!b ? 0 : 2);
579 uint8_t r = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_lsb_cdfs[lsbidx],
580 FF_ARRAY_ELEMS(ff_aac_ac_lsb_cdfs[lsbidx]));
581 a = (a << 1) | (r & 1);
582 b = (b << 1) | ((r >> 1) & 1);
583 }
584
585 /* Dequantize coeffs here */
586 coef[2*i + 0] = a * cbrt(a);
587 coef[2*i + 1] = b * cbrt(b);
588 ff_aac_ac_update_context(state, i, a, b);
589 }
590
591 if (len > 1) {
592 /* "Rewind" bitstream back by 14 bits */
593 int gb_count2 = get_bits_count(&gb2);
594 skip_bits(gb, gb_count2 - gb_count - 14);
595 } else {
596 *gb = gb2;
597 }
598
599 ff_aac_ac_finish(state, i, N);
600
601 for (; i < N/2; i++) {
602 coef[2*i + 0] = 0;
603 coef[2*i + 1] = 0;
604 }
605
606 /* Signs */
607 for (i = 0; i < len; i++) {
608 if (coef[i]) {
609 if (!get_bits1(gb)) /* s */
610 coef[i] *= -1;
611 }
612 }
613
614 return 0;
615}
616
617static int decode_usac_stereo_cplx(AACDecContext *ac, AACUsacStereo *us,
618 ChannelElement *cpe, GetBitContext *gb,
619 int num_window_groups, int indep_flag)
620{
621 int delta_code_time;
622 IndividualChannelStream *ics = &cpe->ch[0].ics;
623
624 if (!get_bits1(gb)) { /* cplx_pred_all */
625 for (int g = 0; g < num_window_groups; g++) {
626 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
627 const uint8_t val = get_bits1(gb);
628 us->pred_used[g*cpe->max_sfb_ste + sfb] = val;
629 if ((sfb + 1) < cpe->max_sfb_ste)
630 us->pred_used[g*cpe->max_sfb_ste + sfb + 1] = val;
631 }
632 }
633 } else {
634 for (int g = 0; g < num_window_groups; g++)
635 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
636 us->pred_used[g*cpe->max_sfb_ste + sfb] = 1;
637 }
638
639 us->pred_dir = get_bits1(gb);
640 us->complex_coef = get_bits1(gb);
641
642 us->use_prev_frame = 0;
643 if (us->complex_coef && !indep_flag)
644 us->use_prev_frame = get_bits1(gb);
645
646 delta_code_time = 0;
647 if (!indep_flag)
648 delta_code_time = get_bits1(gb);
649
650 /* TODO: shouldn't be needed */
651 for (int g = 0; g < num_window_groups; g++) {
652 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
653 float last_alpha_q_re = 0;
654 float last_alpha_q_im = 0;
655 if (delta_code_time) {
656 if (g) {
657 last_alpha_q_re = us->prev_alpha_q_re[(g - 1)*cpe->max_sfb_ste + sfb];
658 last_alpha_q_im = us->prev_alpha_q_im[(g - 1)*cpe->max_sfb_ste + sfb];
659 } else if ((ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) &&
660 ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE ||
661 ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE) {
662 /* The spec doesn't explicitly mention this, but it doesn't make
663 * any other sense otherwise! */
664 last_alpha_q_re = us->prev_alpha_q_re[7*cpe->max_sfb_ste + sfb];
665 last_alpha_q_im = us->prev_alpha_q_im[7*cpe->max_sfb_ste + sfb];
666 } else {
667 last_alpha_q_re = us->prev_alpha_q_re[g*cpe->max_sfb_ste + sfb];
668 last_alpha_q_im = us->prev_alpha_q_im[g*cpe->max_sfb_ste + sfb];
669 }
670 } else {
671 if (sfb) {
672 last_alpha_q_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb - 1];
673 last_alpha_q_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb - 1];
674 }
675 }
676
677 if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
678 int val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
679 last_alpha_q_re += val * 0.1f;
680 if (us->complex_coef) {
681 val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
682 last_alpha_q_im += val * 0.1f;
683 }
684 us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = last_alpha_q_re;
685 us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = last_alpha_q_im;
686 } else {
687 us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = 0;
688 us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = 0;
689 }
690
691 if ((sfb + 1) < cpe->max_sfb_ste) {
692 us->alpha_q_re[g*cpe->max_sfb_ste + sfb + 1] =
693 us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
694 us->alpha_q_im[g*cpe->max_sfb_ste + sfb + 1] =
695 us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
696 }
697 }
698 }
699
700 return 0;
701}
702
703static int setup_sce(AACDecContext *ac, SingleChannelElement *sce,
704 AACUSACConfig *usac)
705{
706 AACUsacElemData *ue = &sce->ue;
707 IndividualChannelStream *ics = &sce->ics;
708
709 /* Setup window parameters */
710 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
711 if (usac->core_frame_len == 768) {
712 ics->swb_offset = ff_swb_offset_96[usac->rate_idx];
713 ics->num_swb = ff_aac_num_swb_96[usac->rate_idx];
714 } else {
715 ics->swb_offset = ff_swb_offset_128[usac->rate_idx];
716 ics->num_swb = ff_aac_num_swb_128[usac->rate_idx];
717 }
718 ics->tns_max_bands = ff_tns_max_bands_128[usac->rate_idx];
719
720 /* Setup scalefactor grouping. 7 bit mask. */
721 ics->num_window_groups = 0;
722 for (int j = 0; j < 7; j++) {
723 ics->group_len[j] = 1;
724 if (ue->scale_factor_grouping & (1 << (6 - j)))
725 ics->group_len[ics->num_window_groups] += 1;
726 else
727 ics->num_window_groups++;
728 }
729
730 ics->group_len[7] = 1;
731 ics->num_window_groups++;
732 ics->num_windows = 8;
733 } else {
734 if (usac->core_frame_len == 768) {
735 ics->swb_offset = ff_swb_offset_768[usac->rate_idx];
736 ics->num_swb = ff_aac_num_swb_768[usac->rate_idx];
737 } else {
738 ics->swb_offset = ff_swb_offset_1024[usac->rate_idx];
739 ics->num_swb = ff_aac_num_swb_1024[usac->rate_idx];
740 }
741 ics->tns_max_bands = ff_tns_max_bands_1024[usac->rate_idx];
742
743 ics->group_len[0] = 1;
744 ics->num_window_groups = 1;
745 ics->num_windows = 1;
746 }
747
748 if (ics->max_sfb > ics->num_swb) {
749 av_log(ac->avctx, AV_LOG_ERROR,
750 "Number of scalefactor bands in group (%d) "
751 "exceeds limit (%d).\n",
752 ics->max_sfb, ics->num_swb);
753 return AVERROR(EINVAL);
754 }
755
756 /* Just some defaults for the band types */
757 for (int i = 0; i < FF_ARRAY_ELEMS(sce->band_type); i++)
758 sce->band_type[i] = ESC_BT;
759
760 return 0;
761}
762
763static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac,
764 AACUsacElemConfig *ec, ChannelElement *cpe,
765 GetBitContext *gb, int indep_flag)
766{
767 int ret, tns_active;
768
769 AACUsacStereo *us = &cpe->us;
770 SingleChannelElement *sce1 = &cpe->ch[0];
771 SingleChannelElement *sce2 = &cpe->ch[1];
772 IndividualChannelStream *ics1 = &sce1->ics;
773 IndividualChannelStream *ics2 = &sce2->ics;
774 AACUsacElemData *ue1 = &sce1->ue;
775 AACUsacElemData *ue2 = &sce2->ue;
776
777 us->common_window = 0;
778 us->common_tw = 0;
779
780 if (!(!ue1->core_mode && !ue2->core_mode))
781 return 0;
782
783 tns_active = get_bits1(gb);
784 us->common_window = get_bits1(gb);
785
786 if (us->common_window) {
787 /* ics_info() */
788 ics1->window_sequence[1] = ics1->window_sequence[0];
789 ics2->window_sequence[1] = ics2->window_sequence[0];
790 ics1->window_sequence[0] = ics2->window_sequence[0] = get_bits(gb, 2);
791
792 ics1->use_kb_window[1] = ics1->use_kb_window[0];
793 ics2->use_kb_window[1] = ics2->use_kb_window[0];
794 ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb);
795
796 if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
797 ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4);
798 ue1->scale_factor_grouping = ue2->scale_factor_grouping = get_bits(gb, 7);
799 } else {
800 ics1->max_sfb = ics2->max_sfb = get_bits(gb, 6);
801 }
802
803 if (!get_bits1(gb)) { /* common_max_sfb */
804 if (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
805 ics2->max_sfb = get_bits(gb, 4);
806 else
807 ics2->max_sfb = get_bits(gb, 6);
808 }
809
810 ret = setup_sce(ac, sce1, usac);
811 if (ret < 0)
812 return ret;
813
814 ret = setup_sce(ac, sce2, usac);
815 if (ret < 0)
816 return ret;
817
818 cpe->max_sfb_ste = FFMAX(ics1->max_sfb, ics2->max_sfb);
819
820 us->ms_mask_mode = get_bits(gb, 2); /* ms_mask_present */
821 memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
822 if (us->ms_mask_mode == 1) {
823 for (int g = 0; g < ics1->num_window_groups; g++)
824 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
825 cpe->ms_mask[g*cpe->max_sfb_ste + sfb] = get_bits1(gb);
826 } else if (us->ms_mask_mode == 2) {
827 memset(cpe->ms_mask, 0xFF, sizeof(cpe->ms_mask));
828 } else if ((us->ms_mask_mode == 3) && !ec->stereo_config_index) {
829 ret = decode_usac_stereo_cplx(ac, us, cpe, gb,
830 ics1->num_window_groups, indep_flag);
831 if (ret < 0)
832 return ret;
833 }
834 }
835
836 if (ec->tw_mdct) {
837 us->common_tw = get_bits1(gb);
838 avpriv_report_missing_feature(ac->avctx,
839 "AAC USAC timewarping");
840 return AVERROR_PATCHWELCOME;
841 }
842
843 sce1->tns.present = sce2->tns.present = 0;
844 if (tns_active) {
845 av_unused int tns_on_lr;
846 int common_tns = 0;
847 if (us->common_window)
848 common_tns = get_bits1(gb);
849
850 tns_on_lr = get_bits1(gb);
851 if (common_tns) {
852 ret = ff_aac_decode_tns(ac, &sce1->tns, gb, ics1);
853 if (ret < 0)
854 return ret;
855 memcpy(&sce2->tns, &sce1->tns, sizeof(sce1->tns));
856 sce2->tns.present = 0;
857 sce1->tns.present = 0;
858 } else {
859 if (get_bits1(gb)) {
860 sce2->tns.present = 1;
861 sce1->tns.present = 1;
862 } else {
863 sce2->tns.present = get_bits1(gb);
864 sce1->tns.present = !sce2->tns.present;
865 }
866 }
867 }
868
869 return 0;
870}
871
872/* 7.2.4 Generation of random signs for spectral noise filling
873 * This function is exactly defined, though we've helped the definition
874 * along with being slightly faster. */
875static inline float noise_random_sign(unsigned int *seed)
876{
877 unsigned int new_seed = *seed = ((*seed) * 69069) + 5;
878 if (((new_seed) & 0x10000) > 0)
879 return -1.f;
880 return +1.f;
881}
882
883static void apply_noise_fill(AACDecContext *ac, SingleChannelElement *sce,
884 AACUsacElemData *ue)
885{
886 float *coef;
887 IndividualChannelStream *ics = &sce->ics;
888
889 float noise_val = pow(2, (ue->noise.level - 14)/3);
890 int noise_offset = ue->noise.offset - 16;
891 int band_off;
892
893 band_off = ff_usac_noise_fill_start_offset[ac->oc[1].m4ac.frame_length_short]
894 [ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE];
895
896 coef = sce->coeffs;
897 for (int g = 0; g < ics->num_window_groups; g++) {
898 unsigned g_len = ics->group_len[g];
899
900 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
901 float *cb = coef + ics->swb_offset[sfb];
902 int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
903 int band_quantized_to_zero = 1;
904
905 if (ics->swb_offset[sfb] < band_off)
906 continue;
907
908 for (int group = 0; group < (unsigned)g_len; group++, cb += 128) {
909 for (int z = 0; z < cb_len; z++) {
910 if (cb[z] == 0)
911 cb[z] = noise_random_sign(&sce->ue.noise.seed) * noise_val;
912 else
913 band_quantized_to_zero = 0;
914 }
915 }
916
917 if (band_quantized_to_zero)
918 sce->sf[g*ics->max_sfb + sfb] += noise_offset;
919 }
920 coef += g_len << 7;
921 }
922}
923
924static void spectrum_scale(AACDecContext *ac, SingleChannelElement *sce,
925 AACUsacElemData *ue)
926{
927 IndividualChannelStream *ics = &sce->ics;
928 float *coef;
929
930 /* Synthesise noise */
931 if (ue->noise.level)
932 apply_noise_fill(ac, sce, ue);
933
934 /* Apply scalefactors */
935 coef = sce->coeffs;
936 for (int g = 0; g < ics->num_window_groups; g++) {
937 unsigned g_len = ics->group_len[g];
938
939 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
940 float *cb = coef + ics->swb_offset[sfb];
941 int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
942 float sf = sce->sf[g*ics->max_sfb + sfb];
943
944 for (int group = 0; group < (unsigned)g_len; group++, cb += 128)
945 ac->fdsp->vector_fmul_scalar(cb, cb, sf, cb_len);
946 }
947 coef += g_len << 7;
948 }
949}
950
951static void complex_stereo_downmix_prev(AACDecContext *ac, ChannelElement *cpe,
952 float *dmix_re)
953{
954 IndividualChannelStream *ics = &cpe->ch[0].ics;
955 int sign = !cpe->us.pred_dir ? +1 : -1;
956 float *coef1 = cpe->ch[0].coeffs;
957 float *coef2 = cpe->ch[1].coeffs;
958
959 for (int g = 0; g < ics->num_window_groups; g++) {
960 unsigned g_len = ics->group_len[g];
961 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
962 int off = ics->swb_offset[sfb];
963 int cb_len = ics->swb_offset[sfb + 1] - off;
964
965 float *c1 = coef1 + off;
966 float *c2 = coef2 + off;
967 float *dm = dmix_re + off;
968
969 for (int group = 0; group < (unsigned)g_len;
970 group++, c1 += 128, c2 += 128, dm += 128) {
971 for (int z = 0; z < cb_len; z++)
972 dm[z] = 0.5*(c1[z] + sign*c2[z]);
973 }
974 }
975
976 coef1 += g_len << 7;
977 coef2 += g_len << 7;
978 dmix_re += g_len << 7;
979 }
980}
981
982static void complex_stereo_downmix_cur(AACDecContext *ac, ChannelElement *cpe,
983 float *dmix_re)
984{
985 AACUsacStereo *us = &cpe->us;
986 IndividualChannelStream *ics = &cpe->ch[0].ics;
987 int sign = !cpe->us.pred_dir ? +1 : -1;
988 float *coef1 = cpe->ch[0].coeffs;
989 float *coef2 = cpe->ch[1].coeffs;
990
991 for (int g = 0; g < ics->num_window_groups; g++) {
992 unsigned g_len = ics->group_len[g];
993 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
994 int off = ics->swb_offset[sfb];
995 int cb_len = ics->swb_offset[sfb + 1] - off;
996
997 float *c1 = coef1 + off;
998 float *c2 = coef2 + off;
999 float *dm = dmix_re + off;
1000
1001 if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
1002 for (int group = 0; group < (unsigned)g_len;
1003 group++, c1 += 128, c2 += 128, dm += 128) {
1004 for (int z = 0; z < cb_len; z++)
1005 dm[z] = 0.5*(c1[z] + sign*c2[z]);
1006 }
1007 } else {
1008 for (int group = 0; group < (unsigned)g_len;
1009 group++, c1 += 128, c2 += 128, dm += 128) {
1010 for (int z = 0; z < cb_len; z++)
1011 dm[z] = c1[z];
1012 }
1013 }
1014 }
1015
1016 coef1 += g_len << 7;
1017 coef2 += g_len << 7;
1018 dmix_re += g_len << 7;
1019 }
1020}
1021
1022static void complex_stereo_interpolate_imag(float *im, float *re, const float f[6],
1023 int len, int factor_even, int factor_odd)
1024{
1025 int i = 0;
1026 float s;
1027
1028 s = f[6]*re[2] + f[5]*re[1] + f[4]*re[0] +
1029 f[3]*re[0] +
1030 f[2]*re[1] + f[1]*re[2] + f[0]*re[3];
1031 im[i] += s*factor_even;
1032
1033 i = 1;
1034 s = f[6]*re[1] + f[5]*re[0] + f[4]*re[0] +
1035 f[3]*re[1] +
1036 f[2]*re[2] + f[1]*re[3] + f[0]*re[4];
1037 im[i] += s*factor_odd;
1038
1039 i = 2;
1040 s = f[6]*re[0] + f[5]*re[0] + f[4]*re[1] +
1041 f[3]*re[2] +
1042 f[2]*re[3] + f[1]*re[4] + f[0]*re[5];
1043
1044 im[i] += s*factor_even;
1045 for (i = 3; i < len - 4; i += 2) {
1046 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1047 f[3]*re[i] +
1048 f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+3];
1049 im[i+0] += s*factor_odd;
1050
1051 s = f[6]*re[i-2] + f[5]*re[i-1] + f[4]*re[i] +
1052 f[3]*re[i+1] +
1053 f[2]*re[i+2] + f[1]*re[i+3] + f[0]*re[i+4];
1054 im[i+1] += s*factor_even;
1055 }
1056
1057 i = len - 3;
1058 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1059 f[3]*re[i] +
1060 f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+2];
1061 im[i] += s*factor_odd;
1062
1063 i = len - 2;
1064 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1065 f[3]*re[i] +
1066 f[2]*re[i+1] + f[1]*re[i+1] + f[0]*re[i];
1067 im[i] += s*factor_even;
1068
1069 i = len - 1;
1070 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1071 f[3]*re[i] +
1072 f[2]*re[i] + f[1]*re[i-1] + f[0]*re[i-2];
1073 im[i] += s*factor_odd;
1074}
1075
1076static void apply_complex_stereo(AACDecContext *ac, ChannelElement *cpe)
1077{
1078 AACUsacStereo *us = &cpe->us;
1079 IndividualChannelStream *ics = &cpe->ch[0].ics;
1080 float *coef1 = cpe->ch[0].coeffs;
1081 float *coef2 = cpe->ch[1].coeffs;
1082 float *dmix_im = us->dmix_im;
1083
1084 for (int g = 0; g < ics->num_window_groups; g++) {
1085 unsigned g_len = ics->group_len[g];
1086 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1087 int off = ics->swb_offset[sfb];
1088 int cb_len = ics->swb_offset[sfb + 1] - off;
1089
1090 float *c1 = coef1 + off;
1091 float *c2 = coef2 + off;
1092 float *dm_im = dmix_im + off;
1093 float alpha_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
1094 float alpha_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
1095
1096 if (!us->pred_used[g*cpe->max_sfb_ste + sfb])
1097 continue;
1098
1099 if (!cpe->us.pred_dir) {
1100 for (int group = 0; group < (unsigned)g_len;
1101 group++, c1 += 128, c2 += 128, dm_im += 128) {
1102 for (int z = 0; z < cb_len; z++) {
1103 float side;
1104 side = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1105 c2[z] = c1[z] - side;
1106 c1[z] = c1[z] + side;
1107 }
1108 }
1109 } else {
1110 for (int group = 0; group < (unsigned)g_len;
1111 group++, c1 += 128, c2 += 128, dm_im += 128) {
1112 for (int z = 0; z < cb_len; z++) {
1113 float mid;
1114 mid = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1115 c2[z] = mid - c1[z];
1116 c1[z] = mid + c1[z];
1117 }
1118 }
1119 }
1120 }
1121
1122 coef1 += g_len << 7;
1123 coef2 += g_len << 7;
1124 dmix_im += g_len << 7;
1125 }
1126}
1127
1128static const float *complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
1129{
1130 int win, shape;
1131 if (!is_prev) {
1132 switch (cpe->ch[0].ics.window_sequence[0]) {
1133 default:
1134 case ONLY_LONG_SEQUENCE:
1135 case EIGHT_SHORT_SEQUENCE:
1136 win = 0;
1137 break;
1138 case LONG_START_SEQUENCE:
1139 win = 1;
1140 break;
1141 case LONG_STOP_SEQUENCE:
1142 win = 2;
1143 break;
1144 }
1145
1146 if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1147 cpe->ch[0].ics.use_kb_window[1] == 0)
1148 shape = 0;
1149 else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1150 cpe->ch[0].ics.use_kb_window[1] == 1)
1151 shape = 1;
1152 else if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1153 cpe->ch[0].ics.use_kb_window[1] == 1)
1154 shape = 2;
1155 else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1156 cpe->ch[0].ics.use_kb_window[1] == 0)
1157 shape = 3;
1158 else
1159 shape = 3;
1160 } else {
1161 win = cpe->ch[0].ics.window_sequence[0] == LONG_STOP_SEQUENCE;
1162 shape = cpe->ch[0].ics.use_kb_window[1];
1163 }
1164
1165 return ff_aac_usac_mdst_filt_cur[win][shape];
1166}
1167
1168static void spectrum_decode(AACDecContext *ac, AACUSACConfig *usac,
1169 ChannelElement *cpe, int nb_channels)
1170{
1171 AACUsacStereo *us = &cpe->us;
1172
1173 for (int ch = 0; ch < nb_channels; ch++) {
1174 SingleChannelElement *sce = &cpe->ch[ch];
1175 AACUsacElemData *ue = &sce->ue;
1176
1177 spectrum_scale(ac, sce, ue);
1178 }
1179
1180 if (nb_channels > 1 && us->common_window) {
1181 if (us->ms_mask_mode == 3) {
1182 const float *filt;
1183 complex_stereo_downmix_cur(ac, cpe, us->dmix_re);
1184 complex_stereo_downmix_prev(ac, cpe, us->prev_dmix_re);
1185
1186 filt = complex_stereo_get_filter(cpe, 0);
1187 complex_stereo_interpolate_imag(us->dmix_im, us->dmix_re, filt,
1188 usac->core_frame_len, 1, 1);
1189 if (us->use_prev_frame) {
1190 filt = complex_stereo_get_filter(cpe, 1);
1191 complex_stereo_interpolate_imag(us->dmix_im, us->prev_dmix_re, filt,
1192 usac->core_frame_len, -1, 1);
1193 }
1194
1195 apply_complex_stereo(ac, cpe);
1196 } else if (us->ms_mask_mode > 0) {
1197 ac->dsp.apply_mid_side_stereo(ac, cpe);
1198 }
1199 }
1200
1201 /* Save coefficients and alpha values for prediction reasons */
1202 if (nb_channels > 1) {
1203 AACUsacStereo *us = &cpe->us;
1204 for (int ch = 0; ch < nb_channels; ch++) {
1205 SingleChannelElement *sce = &cpe->ch[ch];
1206 memcpy(sce->prev_coeffs, sce->coeffs, sizeof(sce->coeffs));
1207 }
1208 memcpy(us->prev_alpha_q_re, us->alpha_q_re, sizeof(us->alpha_q_re));
1209 memcpy(us->prev_alpha_q_im, us->alpha_q_im, sizeof(us->alpha_q_im));
1210 }
1211
1212 for (int ch = 0; ch < nb_channels; ch++) {
1213 SingleChannelElement *sce = &cpe->ch[ch];
1214
1215 /* Apply TNS */
1216 if (sce->tns.present)
1217 ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1218
1219 ac->oc[1].m4ac.frame_length_short ? ac->dsp.imdct_and_windowing_768(ac, sce) :
1220 ac->dsp.imdct_and_windowing(ac, sce);
1221 }
1222}
1223
1224static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac,
1225 AACUsacElemConfig *ec, ChannelElement *che,
1226 GetBitContext *gb, int indep_flag, int nb_channels)
1227{
1228 int ret;
1229 int arith_reset_flag;
1230 AACUsacStereo *us = &che->us;
1231
1232 /* Local symbols */
1233 uint8_t global_gain;
1234
1235 us->common_window = 0;
1236 che->ch[0].tns.present = che->ch[1].tns.present = 0;
1237
1238 for (int ch = 0; ch < nb_channels; ch++) {
1239 SingleChannelElement *sce = &che->ch[ch];
1240 AACUsacElemData *ue = &sce->ue;
1241
1242 ue->core_mode = get_bits1(gb);
1243 }
1244
1245 if (nb_channels == 2) {
1246 ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
1247 if (ret)
1248 return ret;
1249 }
1250
1251 for (int ch = 0; ch < nb_channels; ch++) {
1252 SingleChannelElement *sce = &che->ch[ch];
1253 IndividualChannelStream *ics = &sce->ics;
1254 AACUsacElemData *ue = &sce->ue;
1255
1256 if (ue->core_mode) { /* lpd_channel_stream */
1257 ret = ff_aac_ldp_parse_channel_stream(ac, usac, ue, gb);
1258 if (ret < 0)
1259 return ret;
1260 }
1261
1262 if ((nb_channels == 1) ||
1263 (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
1264 sce->tns.present = get_bits1(gb);
1265
1266 /* fd_channel_stream */
1267 global_gain = get_bits(gb, 8);
1268
1269 ue->noise.level = 0;
1270 if (ec->noise_fill) {
1271 ue->noise.level = get_bits(gb, 3);
1272 ue->noise.offset = get_bits(gb, 5);
1273 }
1274
1275 if (!us->common_window) {
1276 /* ics_info() */
1277 ics->window_sequence[1] = ics->window_sequence[0];
1278 ics->window_sequence[0] = get_bits(gb, 2);
1279 ics->use_kb_window[1] = ics->use_kb_window[0];
1280 ics->use_kb_window[0] = get_bits1(gb);
1281 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1282 ics->max_sfb = get_bits(gb, 4);
1283 ue->scale_factor_grouping = get_bits(gb, 7);
1284 } else {
1285 ics->max_sfb = get_bits(gb, 6);
1286 }
1287
1288 ret = setup_sce(ac, sce, usac);
1289 if (ret < 0)
1290 return ret;
1291 }
1292
1293 if (ec->tw_mdct && !us->common_tw) {
1294 /* tw_data() */
1295 if (get_bits1(gb)) { /* tw_data_present */
1296 /* Time warping is not supported in baseline profile streams. */
1297 avpriv_report_missing_feature(ac->avctx,
1298 "AAC USAC timewarping");
1299 return AVERROR_PATCHWELCOME;
1300 }
1301 }
1302
1303 ret = decode_usac_scale_factors(ac, sce, gb, global_gain);
1304 if (ret < 0)
1305 return ret;
1306
1307 ac->dsp.dequant_scalefactors(sce);
1308
1309 if (sce->tns.present) {
1310 ret = ff_aac_decode_tns(ac, &sce->tns, gb, ics);
1311 if (ret < 0)
1312 return ret;
1313 }
1314
1315 /* ac_spectral_data */
1316 arith_reset_flag = indep_flag;
1317 if (!arith_reset_flag)
1318 arith_reset_flag = get_bits1(gb);
1319
1320 /* Decode coeffs */
1321 memset(&sce->coeffs[0], 0, 1024*sizeof(float));
1322 for (int win = 0; win < ics->num_windows; win++) {
1323 int lg = ics->swb_offset[ics->max_sfb];
1324 int N;
1325 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
1326 N = usac->core_frame_len / 8;
1327 else
1328 N = usac->core_frame_len;
1329
1330 ret = decode_spectrum_and_dequant_ac(ac, sce->coeffs + win*128, gb,
1331 sce->sf, &ue->ac,
1332 arith_reset_flag && (win == 0),
1333 lg, N);
1334 if (ret < 0)
1335 return ret;
1336 }
1337
1338 if (get_bits1(gb)) { /* fac_data_present */
1339 const uint16_t len_8 = usac->core_frame_len / 8;
1340 const uint16_t len_16 = usac->core_frame_len / 16;
1341 const uint16_t fac_len = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? len_8 : len_16;
1342 ret = ff_aac_parse_fac_data(ue, gb, 1, fac_len);
1343 if (ret < 0)
1344 return ret;
1345 }
1346 }
1347
1348 spectrum_decode(ac, usac, che, nb_channels);
1349
1350 return 0;
1351}
1352
1353static int parse_audio_preroll(AACDecContext *ac, GetBitContext *gb)
1354{
1355 int ret = 0;
1356 GetBitContext gbc;
1357 OutputConfiguration *oc = &ac->oc[1];
1358 MPEG4AudioConfig *m4ac = &oc->m4ac;
1359 MPEG4AudioConfig m4ac_bak = oc->m4ac;
1360 uint8_t temp_data[512];
1361 uint8_t *tmp_buf = temp_data;
1362 size_t tmp_buf_size = sizeof(temp_data);
1363
1364 av_unused int crossfade;
1365 int num_preroll_frames;
1366
1367 int config_len = get_escaped_value(gb, 4, 4, 8);
1368
1369 /* Implementations are free to pad the config to any length, so use a
1370 * different reader for this. */
1371 gbc = *gb;
1372 ret = ff_aac_usac_config_decode(ac, ac->avctx, &gbc, oc, m4ac->chan_config);
1373 if (ret < 0) {
1374 *m4ac = m4ac_bak;
1375 return ret;
1376 } else {
1377 ac->oc[1].m4ac.chan_config = 0;
1378 }
1379
1380 /* 7.18.3.3 Bitrate adaption
1381 * If configuration didn't change after applying preroll, continue
1382 * without decoding it. */
1383 if (!memcmp(m4ac, &m4ac_bak, sizeof(m4ac_bak)))
1384 return 0;
1385
1386 skip_bits_long(gb, config_len*8);
1387
1388 crossfade = get_bits1(gb); /* applyCrossfade */
1389 skip_bits1(gb); /* reserved */
1390 num_preroll_frames = get_escaped_value(gb, 2, 4, 0); /* numPreRollFrames */
1391
1392 for (int i = 0; i < num_preroll_frames; i++) {
1393 int got_frame_ptr = 0;
1394 int au_len = get_escaped_value(gb, 16, 16, 0);
1395
1396 if (au_len*8 > tmp_buf_size) {
1397 uint8_t *tmp2;
1398 tmp_buf = tmp_buf == temp_data ? NULL : tmp_buf;
1399 tmp2 = realloc(tmp_buf, au_len*8);
1400 if (!tmp2) {
1401 if (tmp_buf != temp_data)
1402 av_free(tmp_buf);
1403 return AVERROR(ENOMEM);
1404 }
1405 tmp_buf = tmp2;
1406 }
1407
1408 /* Byte alignment is not guaranteed. */
1409 for (int i = 0; i < au_len; i++)
1410 tmp_buf[i] = get_bits(gb, 8);
1411
1412 ret = init_get_bits8(&gbc, tmp_buf, au_len);
1413 if (ret < 0)
1414 break;
1415
1416 ret = ff_aac_usac_decode_frame(ac->avctx, ac, &gbc, &got_frame_ptr);
1417 if (ret < 0)
1418 break;
1419 }
1420
1421 if (tmp_buf != temp_data)
1422 av_free(tmp_buf);
1423
1424 return 0;
1425}
1426
1427static int parse_ext_ele(AACDecContext *ac, AACUsacElemConfig *e,
1428 GetBitContext *gb)
1429{
1430 uint8_t *tmp;
1431 uint8_t pl_frag_start = 1;
1432 uint8_t pl_frag_end = 1;
1433 uint32_t len;
1434
1435 if (!get_bits1(gb)) /* usacExtElementPresent */
1436 return 0;
1437
1438 if (get_bits1(gb)) { /* usacExtElementUseDefaultLength */
1439 len = e->ext.default_len;
1440 } else {
1441 len = get_bits(gb, 8); /* usacExtElementPayloadLength */
1442 if (len == 255)
1443 len += get_bits(gb, 16) - 2;
1444 }
1445
1446 if (!len)
1447 return 0;
1448
1449 if (e->ext.payload_frag) {
1450 pl_frag_start = get_bits1(gb); /* usacExtElementStart */
1451 pl_frag_end = get_bits1(gb); /* usacExtElementStop */
1452 }
1453
1454 if (pl_frag_start)
1455 e->ext.pl_data_offset = 0;
1456
1457 /* If an extension starts and ends this packet, we can directly use it */
1458 if (!(pl_frag_start && pl_frag_end)) {
1459 tmp = av_realloc(e->ext.pl_data, e->ext.pl_data_offset + len);
1460 if (!tmp) {
1461 av_free(e->ext.pl_data);
1462 return AVERROR(ENOMEM);
1463 }
1464 e->ext.pl_data = tmp;
1465
1466 /* Readout data to a buffer */
1467 for (int i = 0; i < len; i++)
1468 e->ext.pl_data[e->ext.pl_data_offset + i] = get_bits(gb, 8);
1469 }
1470
1471 e->ext.pl_data_offset += len;
1472
1473 if (pl_frag_end) {
1474 int ret = 0;
1475 int start_bits = get_bits_count(gb);
1476 const int pl_len = e->ext.pl_data_offset;
1477 GetBitContext *gb2 = gb;
1478 GetBitContext gbc;
1479 if (!(pl_frag_start && pl_frag_end)) {
1480 ret = init_get_bits8(&gbc, e->ext.pl_data, pl_len);
1481 if (ret < 0)
1482 return ret;
1483
1484 gb2 = &gbc;
1485 }
1486
1487 switch (e->ext.type) {
1488 case ID_EXT_ELE_FILL:
1489 /* Filler elements have no usable payload */
1490 break;
1491 case ID_EXT_ELE_AUDIOPREROLL:
1492 ret = parse_audio_preroll(ac, gb2);
1493 break;
1494 default:
1495 /* This should never happen */
1496 av_assert0(0);
1497 }
1498 av_freep(&e->ext.pl_data);
1499 if (ret < 0)
1500 return ret;
1501
1502 skip_bits_long(gb, pl_len*8 - (get_bits_count(gb) - start_bits));
1503 }
1504
1505 return 0;
1506}
1507
1508int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
1509 GetBitContext *gb, int *got_frame_ptr)
1510{
1511 int ret, nb_ch_el, is_dmono = 0;
1512 int indep_flag, samples = 0;
1513 int audio_found = 0, sce_count = 0;
1514 AVFrame *frame = ac->frame;
1515
1516 ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
1517 ac->oc[1].status, 0);
1518
1519 indep_flag = get_bits1(gb);
1520
1521 nb_ch_el = 0;
1522 for (int i = 0; i < ac->oc[1].usac.nb_elems; i++) {
1523 AACUsacElemConfig *e = &ac->oc[1].usac.elems[i];
1524 ChannelElement *che;
1525
1526 switch (e->type) {
1527 case ID_USAC_LFE:
1528 /* Fallthrough */
1529 case ID_USAC_SCE:
1530 che = ff_aac_get_che(ac, TYPE_SCE, nb_ch_el++);
1531 if (!che) {
1532 av_log(ac->avctx, AV_LOG_ERROR,
1533 "channel element %d.%d is not allocated\n",
1534 TYPE_SCE, nb_ch_el - 1);
1535 return AVERROR_INVALIDDATA;
1536 }
1537
1538 ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1539 indep_flag, 1);
1540 if (ret < 0)
1541 return ret;
1542
1543 sce_count++;
1544 audio_found = 1;
1545 che->present = 1;
1546 samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
1547 break;
1548 case ID_USAC_CPE:
1549 che = ff_aac_get_che(ac, TYPE_CPE, nb_ch_el++);
1550 if (!che) {
1551 av_log(ac->avctx, AV_LOG_ERROR,
1552 "channel element %d.%d is not allocated\n",
1553 TYPE_SCE, nb_ch_el - 1);
1554 return AVERROR_INVALIDDATA;
1555 }
1556
1557 ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
1558 indep_flag, 2);
1559 if (ret < 0)
1560 return ret;
1561
1562 audio_found = 1;
1563 che->present = 1;
1564 samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
1565 break;
1566 case ID_USAC_EXT:
1567 ret = parse_ext_ele(ac, e, gb);
1568 if (ret < 0)
1569 return ret;
1570 break;
1571 }
1572 }
1573
1574 if (ac->oc[1].status && audio_found) {
1575 avctx->sample_rate = ac->oc[1].m4ac.sample_rate;
1576 avctx->frame_size = samples;
1577 ac->oc[1].status = OC_LOCKED;
1578 }
1579
1580 if (!frame->data[0] && samples) {
1581 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
1582 return AVERROR_INVALIDDATA;
1583 }
1584
1585 if (samples) {
1586 frame->nb_samples = samples;
1587 frame->sample_rate = avctx->sample_rate;
1588 frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1589 *got_frame_ptr = 1;
1590 } else {
1591 av_frame_unref(ac->frame);
1592 frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
1593 *got_frame_ptr = 0;
1594 }
1595
1596 /* for dual-mono audio (SCE + SCE) */
1597 is_dmono = ac->dmono_mode && sce_count == 2 &&
1598 !av_channel_layout_compare(&ac->oc[1].ch_layout,
1599 &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO);
1600 if (is_dmono) {
1601 if (ac->dmono_mode == 1)
1602 frame->data[1] = frame->data[0];
1603 else if (ac->dmono_mode == 2)
1604 frame->data[0] = frame->data[1];
1605 }
1606
1607 return 0;
1608}
libavcodec/aac/aacdec_usac.h+37−0
@@ -0,0 +1,37 @@
1/*
2 * Copyright (c) 2024 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVCODEC_AAC_AACDEC_USAC_H
22#define AVCODEC_AAC_AACDEC_USAC_H
23
24#include "aacdec.h"
25
26#include "libavcodec/get_bits.h"
27
28int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx,
29 GetBitContext *gb, OutputConfiguration *oc,
30 int channel_config);
31
32int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc);
33
34int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac,
35 GetBitContext *gb, int *got_frame_ptr);
36
37#endif /* AVCODEC_AAC_AACDEC_USAC_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t