43
42
#include "aactab.h"
44
43
#include "aacenc.h"
44
#include "aacenctab.h"
45
#include "aacenc_utils.h"
46
47
#include "psymodel.h"
48
#define AAC_MAX_CHANNELS 6
50
#define ERROR_IF(cond, ...) \
52
av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
53
return AVERROR(EINVAL); \
56
#define WARN_IF(cond, ...) \
58
av_log(avctx, AV_LOG_WARNING, __VA_ARGS__); \
61
float ff_aac_pow34sf_tab[428];
63
static const uint8_t swb_size_1024_96[] = {
64
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
65
12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
66
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
69
static const uint8_t swb_size_1024_64[] = {
70
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
71
12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
72
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
75
static const uint8_t swb_size_1024_48[] = {
76
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
77
12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
78
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
82
static const uint8_t swb_size_1024_32[] = {
83
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
84
12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
85
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
88
static const uint8_t swb_size_1024_24[] = {
89
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
90
12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
91
32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
94
static const uint8_t swb_size_1024_16[] = {
95
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
96
12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
97
32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
100
static const uint8_t swb_size_1024_8[] = {
101
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
102
16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
103
32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
106
static const uint8_t *swb_size_1024[] = {
107
swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
108
swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
109
swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
110
swb_size_1024_16, swb_size_1024_16, swb_size_1024_8,
114
static const uint8_t swb_size_128_96[] = {
115
4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
118
static const uint8_t swb_size_128_48[] = {
119
4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
122
static const uint8_t swb_size_128_24[] = {
123
4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
126
static const uint8_t swb_size_128_16[] = {
127
4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
130
static const uint8_t swb_size_128_8[] = {
131
4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
134
static const uint8_t *swb_size_128[] = {
135
/* the last entry on the following row is swb_size_128_64 but is a
136
duplicate of swb_size_128_96 */
137
swb_size_128_96, swb_size_128_96, swb_size_128_96,
138
swb_size_128_48, swb_size_128_48, swb_size_128_48,
139
swb_size_128_24, swb_size_128_24, swb_size_128_16,
140
swb_size_128_16, swb_size_128_16, swb_size_128_8,
144
/** default channel configurations */
145
static const uint8_t aac_chan_configs[6][5] = {
146
{1, TYPE_SCE}, // 1 channel - single channel element
147
{1, TYPE_CPE}, // 2 channels - channel pair
148
{2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
149
{3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
150
{3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
151
{4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
155
* Table to remap channels from libavcodec's default order to AAC order.
157
static const uint8_t aac_chan_maps[AAC_MAX_CHANNELS][AAC_MAX_CHANNELS] = {
163
{ 2, 0, 1, 4, 5, 3 },
167
50
* Make AAC audio config object.
168
51
* @see 1.6.2.1 "Syntax - AudioSpecificConfig"
631
for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
633
s->cur_channel = start_ch + ch;
634
if (s->options.pns && s->coder->search_for_pns)
635
s->coder->search_for_pns(s, avctx, sce);
636
if (s->options.tns && s->coder->search_for_tns)
637
s->coder->search_for_tns(s, sce);
638
if (s->options.tns && s->coder->apply_tns_filt)
639
s->coder->apply_tns_filt(s, sce);
640
if (sce->tns.present)
638
643
s->cur_channel = start_ch;
639
if (s->options.stereo_mode && cpe->common_window) {
640
if (s->options.stereo_mode > 0) {
641
IndividualChannelStream *ics = &cpe->ch[0].ics;
642
for (w = 0; w < ics->num_windows; w += ics->group_len[w])
643
for (g = 0; g < ics->num_swb; g++)
644
cpe->ms_mask[w*16+g] = 1;
645
} else if (s->coder->search_for_ms) {
646
s->coder->search_for_ms(s, cpe, s->lambda);
644
if (s->options.intensity_stereo) { /* Intensity Stereo */
645
if (s->coder->search_for_is)
646
s->coder->search_for_is(s, avctx, cpe);
647
if (cpe->is_mode) is_mode = 1;
648
apply_intensity_stereo(cpe);
650
if (s->options.pred) { /* Prediction */
651
for (ch = 0; ch < chans; ch++) {
653
s->cur_channel = start_ch + ch;
654
if (s->options.pred && s->coder->search_for_pred)
655
s->coder->search_for_pred(s, sce);
656
if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
658
if (s->coder->adjust_common_prediction)
659
s->coder->adjust_common_prediction(s, cpe);
660
for (ch = 0; ch < chans; ch++) {
662
s->cur_channel = start_ch + ch;
663
if (s->options.pred && s->coder->apply_main_pred)
664
s->coder->apply_main_pred(s, sce);
666
s->cur_channel = start_ch;
668
if (s->options.stereo_mode) { /* Mid/Side stereo */
669
if (s->options.stereo_mode == -1 && s->coder->search_for_ms)
670
s->coder->search_for_ms(s, cpe);
671
else if (cpe->common_window)
672
memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
673
for (w = 0; w < 128; w++)
674
cpe->ms_mask[w] = cpe->is_mask[w] ? 0 : cpe->ms_mask[w];
675
apply_mid_side_stereo(cpe);
649
677
adjust_frame_information(cpe, chans);
650
678
if (chans == 2) {
651
679
put_bits(&s->pb, 1, cpe->common_window);
652
680
if (cpe->common_window) {
653
681
put_ics_info(s, &cpe->ch[0].ics);
682
if (s->coder->encode_main_pred)
683
s->coder->encode_main_pred(s, &cpe->ch[0]);
654
684
encode_ms_info(&s->pb, cpe);
655
685
if (cpe->ms_mode) ms_mode = 1;
837
879
{"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
838
880
{"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
839
881
{"ms_force", "Force Mid/Side for the whole frame if possible", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
840
{"aac_coder", "", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "aac_coder"},
882
{"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.aac_coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "aac_coder"},
841
883
{"faac", "FAAC-inspired method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAAC}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
842
884
{"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
843
885
{"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
844
886
{"fast", "Constant quantizer", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_coder"},
845
{"aac_pns", "Perceptual Noise Substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_pns"},
846
{"disable", "Disable PNS", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
847
{"enable", "Enable PNS (Proof of concept)", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
887
{"aac_pns", "Perceptual Noise Substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AACENC_FLAGS, "aac_pns"},
888
{"disable", "Disable perceptual noise substitution", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
889
{"enable", "Enable perceptual noise substitution", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pns"},
890
{"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AACENC_FLAGS, "intensity_stereo"},
891
{"disable", "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
892
{"enable", "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
893
{"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_tns"},
894
{"disable", "Disable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
895
{"enable", "Enable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
896
{"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_pred"},
897
{"disable", "Disable AAC-Main prediction", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pred"},
898
{"enable", "Enable AAC-Main prediction", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_pred"},