History

aacenc_pred: rework the way prediction is done

This commit completely alters the algorithm of prediction. The original commit which introduced prediction was completely incorrect to even remotely care about what the actual coefficients contain or whether any options were enabled. Not my actual fault. This commit treats prediction the way the decoder does and expects to do: like lossy encryption. Everything related to prediction now happens at the very end but just before quantization and encoding of coefficients. On the decoder side, prediction happens before anything has had a chance to even access the coefficients. Also the original implementation had problems because it actually touched the band_type of special bands which already had their scalefactor indices marked and it's a wonder the asserion wasn't triggered when transmitting those. Overall, this now drastically increases audio quality and you should think about enabling it if you don't plan on playing anything encoded on really old low power ultra-embedded devices since they might not support decoding of prediction or AAC-Main. Though the specifications were written ages ago and as times change so do the FLOPS.

Rostislav Pehlivanov committed 11 years ago

Commit 44ddee91 parent 949a4896 files · +190 −225Browse files

Unified · Split

6 files
  1. libavcodec/aac.h +1 −1
  2. libavcodec/aaccoder.c +0 −4
  3. libavcodec/aacenc.c +19 −22
  4. libavcodec/aacenc.h +0 −1
  5. libavcodec/aacenc_pred.c +169 −192
  6. libavcodec/aacenc_pred.h +1 −5
libavcodec/aac.h+1−1
@@ -247,7 +247,7 @@ typedef struct SingleChannelElement { · 246 unmodified lines
247 TemporalNoiseShaping tns;247 TemporalNoiseShaping tns;
248 Pulse pulse;248 Pulse pulse;
249 enum BandType band_type[128]; ///< band types249 enum BandType band_type[128]; ///< band types
250 enum BandType orig_band_type[128]; ///< band type backups for undoing prediction250 enum BandType band_alt[128]; ///< alternative band type (used by encoder)
251 int band_type_run_end[120]; ///< band type run end points251 int band_type_run_end[120]; ///< band type run end points
252 INTFLOAT sf[120]; ///< scalefactors252 INTFLOAT sf[120]; ///< scalefactors
253 int sf_idx[128]; ///< scalefactor indices (used by encoder)253 int sf_idx[128]; ///< scalefactor indices (used by encoder)
libavcodec/aaccoder.c+0−4
@@ -964,7 +964,6 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { · 963 unmodified lines
964 ff_aac_encode_main_pred,964 ff_aac_encode_main_pred,
965 ff_aac_adjust_common_prediction,965 ff_aac_adjust_common_prediction,
966 ff_aac_apply_main_pred,966 ff_aac_apply_main_pred,
967 ff_aac_update_main_pred,
968 set_special_band_scalefactors,967 set_special_band_scalefactors,
969 search_for_pns,968 search_for_pns,
970 ff_aac_search_for_tns,969 ff_aac_search_for_tns,
@@ -980,7 +979,6 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { · 9 unmodified lines
980 ff_aac_encode_main_pred,979 ff_aac_encode_main_pred,
981 ff_aac_adjust_common_prediction,980 ff_aac_adjust_common_prediction,
982 ff_aac_apply_main_pred,981 ff_aac_apply_main_pred,
983 ff_aac_update_main_pred,
984 set_special_band_scalefactors,982 set_special_band_scalefactors,
985 search_for_pns,983 search_for_pns,
986 ff_aac_search_for_tns,984 ff_aac_search_for_tns,
@@ -996,7 +994,6 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { · 9 unmodified lines
996 ff_aac_encode_main_pred,994 ff_aac_encode_main_pred,
997 ff_aac_adjust_common_prediction,995 ff_aac_adjust_common_prediction,
998 ff_aac_apply_main_pred,996 ff_aac_apply_main_pred,
999 ff_aac_update_main_pred,
1000 set_special_band_scalefactors,997 set_special_band_scalefactors,
1001 search_for_pns,998 search_for_pns,
1002 ff_aac_search_for_tns,999 ff_aac_search_for_tns,
@@ -1012,7 +1009,6 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = { · 9 unmodified lines
1012 ff_aac_encode_main_pred,1009 ff_aac_encode_main_pred,
1013 ff_aac_adjust_common_prediction,1010 ff_aac_adjust_common_prediction,
1014 ff_aac_apply_main_pred,1011 ff_aac_apply_main_pred,
1015 ff_aac_update_main_pred,
1016 set_special_band_scalefactors,1012 set_special_band_scalefactors,
1017 search_for_pns,1013 search_for_pns,
1018 ff_aac_search_for_tns,1014 ff_aac_search_for_tns,
libavcodec/aacenc.c+19−22
@@ -354,15 +354,15 @@ static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) · 353 unmodified lines
354 start += sce->ics.swb_sizes[i];354 start += sce->ics.swb_sizes[i];
355 continue;355 continue;
356 }356 }
357 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++)357 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
358 s->coder->quantize_and_encode_band(s, &s->pb,358 s->coder->quantize_and_encode_band(s, &s->pb,
359 &sce->coeffs[start + w2*128],359 &sce->coeffs[start + w2*128],
360 &sce->pqcoeffs[start + w2*128],360 NULL, sce->ics.swb_sizes[i],
361 sce->ics.swb_sizes[i],
362 sce->sf_idx[w*16 + i],361 sce->sf_idx[w*16 + i],
363 sce->band_type[w*16 + i],362 sce->band_type[w*16 + i],
364 s->lambda,363 s->lambda,
365 sce->ics.window_clipping[w]);364 sce->ics.window_clipping[w]);
365 }
366 start += sce->ics.swb_sizes[i];366 start += sce->ics.swb_sizes[i];
367 }367 }
368 }368 }
@@ -609,12 +609,8 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 240 unmodified lines
609 s->coder->search_for_pns(s, avctx, sce);609 s->coder->search_for_pns(s, avctx, sce);
610 if (s->options.tns && s->coder->search_for_tns)610 if (s->options.tns && s->coder->search_for_tns)
611 s->coder->search_for_tns(s, sce);611 s->coder->search_for_tns(s, sce);
612 if (s->options.pred && s->coder->search_for_pred)
613 s->coder->search_for_pred(s, sce);
614 if (sce->tns.present)612 if (sce->tns.present)
615 tns_mode = 1;613 tns_mode = 1;
616 if (sce->ics.predictor_present)
617 pred_mode = 1;
618 }614 }
619 s->cur_channel = start_ch;615 s->cur_channel = start_ch;
620 if (s->options.stereo_mode && cpe->common_window) {616 if (s->options.stereo_mode && cpe->common_window) {
@@ -631,15 +627,26 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 10 unmodified lines
631 s->coder->search_for_is(s, avctx, cpe);627 s->coder->search_for_is(s, avctx, cpe);
632 if (cpe->is_mode) is_mode = 1;628 if (cpe->is_mode) is_mode = 1;
633 }629 }
634 if (s->options.pred && s->coder->adjust_common_prediction)
635 s->coder->adjust_common_prediction(s, cpe);
636 if (s->coder->set_special_band_scalefactors)630 if (s->coder->set_special_band_scalefactors)
637 for (ch = 0; ch < chans; ch++)631 for (ch = 0; ch < chans; ch++)
638 s->coder->set_special_band_scalefactors(s, &cpe->ch[ch]);632 s->coder->set_special_band_scalefactors(s, &cpe->ch[ch]);
639 if (s->options.pred && s->coder->apply_main_pred)
640 for (ch = 0; ch < chans; ch++)
641 s->coder->apply_main_pred(s, &cpe->ch[ch]);
642 adjust_frame_information(cpe, chans);633 adjust_frame_information(cpe, chans);
634 for (ch = 0; ch < chans; ch++) {
635 sce = &cpe->ch[ch];
636 s->cur_channel = start_ch + ch;
637 if (s->options.pred && s->coder->search_for_pred)
638 s->coder->search_for_pred(s, sce);
639 if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
640 }
641 if (s->options.pred && s->coder->adjust_common_prediction)
642 s->coder->adjust_common_prediction(s, cpe);
643 for (ch = 0; ch < chans; ch++) {
644 sce = &cpe->ch[ch];
645 s->cur_channel = start_ch + ch;
646 if (s->options.pred && s->coder->apply_main_pred)
647 s->coder->apply_main_pred(s, sce);
648 }
649 s->cur_channel = start_ch;
643 if (chans == 2) {650 if (chans == 2) {
644 put_bits(&s->pb, 1, cpe->common_window);651 put_bits(&s->pb, 1, cpe->common_window);
645 if (cpe->common_window) {652 if (cpe->common_window) {
@@ -676,16 +683,6 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 30 unmodified lines
676 683
677 } while (1);684 } while (1);
678 685
679 // update predictor state
680 if (s->options.pred && s->coder->update_main_pred) {
681 for (i = 0; i < s->chan_map[0]; i++) {
682 cpe = &s->cpe[i];
683 for (ch = 0; ch < chans; ch++)
684 s->coder->update_main_pred(s, &cpe->ch[ch],
685 (cpe->common_window && !ch) ? cpe : NULL);
686 }
687 }
688
689 put_bits(&s->pb, 3, TYPE_END);686 put_bits(&s->pb, 3, TYPE_END);
690 flush_put_bits(&s->pb);687 flush_put_bits(&s->pb);
691 avctx->frame_bits = put_bits_count(&s->pb);688 avctx->frame_bits = put_bits_count(&s->pb);
libavcodec/aacenc.h+0−1
@@ -63,7 +63,6 @@ typedef struct AACCoefficientsEncoder { · 62 unmodified lines
63 void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);63 void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
64 void (*adjust_common_prediction)(struct AACEncContext *s, ChannelElement *cpe);64 void (*adjust_common_prediction)(struct AACEncContext *s, ChannelElement *cpe);
65 void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);65 void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
66 void (*update_main_pred)(struct AACEncContext *s, SingleChannelElement *sce, ChannelElement *cpe);
67 void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);66 void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
68 void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);67 void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
69 void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);68 void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
libavcodec/aacenc_pred.c+169−192
@@ -21,15 +21,22 @@ · 20 unmodified lines
21 21
22/**22/**
23 * @file23 * @file
24 * AAC encoder main prediction24 * AAC encoder Intensity Stereo
25 * @author Rostislav Pehlivanov ( atomnuker gmail com )25 * @author Rostislav Pehlivanov ( atomnuker gmail com )
26 */26 */
27 27
28#include "aactab.h"28#include "aactab.h"
29#include "aacenc_pred.h"29#include "aacenc_pred.h"
30#include "aacenc_utils.h"30#include "aacenc_utils.h"
31#include "aacenc_is.h" /* <- Needed for common window distortions */
31#include "aacenc_quantization.h"32#include "aacenc_quantization.h"
32 33
34#define RESTORE_PRED(sce, sfb) \
35 if (sce->ics.prediction_used[sfb]) {\
36 sce->ics.prediction_used[sfb] = 0;\
37 sce->band_type[sfb] = sce->band_alt[sfb];\
38 }
39
33static inline float flt16_round(float pf)40static inline float flt16_round(float pf)
34{41{
35 union av_intfloat32 tmp;42 union av_intfloat32 tmp;
@@ -54,73 +61,57 @@ static inline float flt16_trunc(float pf) · 18 unmodified lines
54 return pun.f;61 return pun.f;
55}62}
56 63
57static inline void predict(PredictorState *ps, float *coef, float *rcoef,64static inline void predict(PredictorState *ps, float *coef, float *rcoef, int set)
58 int output_enable)
59{65{
60 const float a = 0.953125; // 61.0 / 64
61 float k2;66 float k2;
62 float r0 = ps->r0, r1 = ps->r1;
63 float cor0 = ps->cor0, cor1 = ps->cor1;
64 float var0 = ps->var0, var1 = ps->var1;
65
66 ps->k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
67 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
68
69 ps->x_est = flt16_round(ps->k1*r0 + k2*r1);
70
71 if (output_enable)
72 *coef -= ps->x_est;
73 else
74 *rcoef = *coef - ps->x_est;
75}
76
77static inline void update_predictor(PredictorState *ps, float qcoef)
78{
79 const float alpha = 0.90625; // 29.0 / 32
80 const float a = 0.953125; // 61.0 / 6467 const float a = 0.953125; // 61.0 / 64
81 float k1 = ps->k1;68 const float alpha = 0.90625; // 29.0 / 32
82 float r0 = ps->r0;69 const float k1 = ps->k1;
83 float r1 = ps->r1;70 const float r0 = ps->r0, r1 = ps->r1;
84 float e0 = qcoef + ps->x_est;71 const float cor0 = ps->cor0, cor1 = ps->cor1;
85 float e1 = e0 - k1 * r0;72 const float var0 = ps->var0, var1 = ps->var1;
86 float cor0 = ps->cor0, cor1 = ps->cor1;73 const float e0 = *coef - ps->x_est;
87 float var0 = ps->var0, var1 = ps->var1;74 const float e1 = e0 - k1 * r0;
75
76 if (set)
77 *coef = e0;
88 78
89 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);79 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
90 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));80 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
91 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);81 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
92 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));82 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
83 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
84 ps->r0 = flt16_trunc(a * e0);
93 85
94 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));86 /* Prediction for next frame */
95 ps->r0 = flt16_trunc(a * e0);87 ps->k1 = ps->var0 > 1 ? ps->cor0 * flt16_even(a / ps->var0) : 0;
88 k2 = ps->var1 > 1 ? ps->cor1 * flt16_even(a / ps->var1) : 0;
89 *rcoef = ps->x_est = flt16_round(ps->k1*ps->r0 + k2*ps->r1);
96}90}
97 91
98static inline void reset_predict_state(PredictorState *ps)92static inline void reset_predict_state(PredictorState *ps)
99{93{
100 ps->r0 = 0.0f;94 ps->r0 = 0.0f;
101 ps->r1 = 0.0f;95 ps->r1 = 0.0f;
102 ps->cor0 = 0.0f;96 ps->k1 = 0.0f;
103 ps->cor1 = 0.0f;97 ps->cor0 = 0.0f;
104 ps->var0 = 1.0f;98 ps->cor1 = 0.0f;
105 ps->var1 = 1.0f;99 ps->var0 = 1.0f;
106 ps->k1 = 0.0f;100 ps->var1 = 1.0f;
107 ps->x_est= 0.0f;101 ps->x_est = 0.0f;
108}102}
109 103
110static inline void reset_all_predictors(SingleChannelElement *sce)104static inline void reset_all_predictors(PredictorState *ps)
111{105{
112 int i;106 int i;
113 for (i = 0; i < MAX_PREDICTORS; i++)107 for (i = 0; i < MAX_PREDICTORS; i++)
114 reset_predict_state(&sce->predictor_state[i]);108 reset_predict_state(&ps[i]);
115 for (i = 1; i < 31; i++)
116 sce->ics.predictor_reset_count[i] = 0;
117}109}
118 110
119static inline void reset_predictor_group(SingleChannelElement *sce, int group_num)111static inline void reset_predictor_group(SingleChannelElement *sce, int group_num)
120{112{
121 int i;113 int i;
122 PredictorState *ps = sce->predictor_state;114 PredictorState *ps = sce->predictor_state;
123 sce->ics.predictor_reset_count[group_num] = 0;
124 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)115 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
125 reset_predict_state(&ps[i]);116 reset_predict_state(&ps[i]);
126}117}
@@ -128,136 +119,89 @@ static inline void reset_predictor_group(SingleChannelElement *sce, int group_nu · 1 unmodified line
128void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce)119void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce)
129{120{
130 int sfb, k;121 int sfb, k;
122 const int pmax = FFMIN(sce->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
131 123
132 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {124 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
133 for (sfb = 0; sfb < ff_aac_pred_sfb_max[s->samplerate_index]; sfb++) {125 for (sfb = 0; sfb < pmax; sfb++) {
134 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++)126 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
135 predict(&sce->predictor_state[k], &sce->coeffs[k], &sce->prcoeffs[k],127 predict(&sce->predictor_state[k], &sce->coeffs[k], &sce->prcoeffs[k],
136 (sce->ics.predictor_present && sce->ics.prediction_used[sfb]));128 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
137 }
138 }
139}
140
141static void decode_joint_stereo(ChannelElement *cpe)
142{
143 int i, w, w2, g;
144 SingleChannelElement *sce0 = &cpe->ch[0];
145 SingleChannelElement *sce1 = &cpe->ch[1];
146 IndividualChannelStream *ics;
147
148 for (i = 0; i < MAX_PREDICTORS; i++)
149 sce0->prcoeffs[i] = sce0->predictor_state[i].x_est;
150
151 ics = &sce0->ics;
152 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
153 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
154 int start = (w+w2) * 128;
155 for (g = 0; g < ics->num_swb; g++) {
156 int sfb = w*16 + g;
157 //apply Intensity stereo coeffs transformation
158 if (cpe->is_mask[sfb]) {
159 int p = -1 + 2 * (sce1->band_type[sfb] - 14);
160 float rscale = ff_aac_pow2sf_tab[-sce1->sf_idx[sfb] + POW_SF2_ZERO];
161 p *= 1 - 2 * cpe->ms_mask[sfb];
162 for (i = 0; i < ics->swb_sizes[g]; i++) {
163 sce0->pqcoeffs[start+i] = (sce0->prcoeffs[start+i] + p*sce0->pqcoeffs[start+i]) * rscale;
164 }
165 } else if (cpe->ms_mask[sfb] &&
166 sce0->band_type[sfb] < NOISE_BT &&
167 sce1->band_type[sfb] < NOISE_BT) {
168 for (i = 0; i < ics->swb_sizes[g]; i++) {
169 float L = sce0->pqcoeffs[start+i] + sce1->pqcoeffs[start+i];
170 float R = sce0->pqcoeffs[start+i] - sce1->pqcoeffs[start+i];
171 sce0->pqcoeffs[start+i] = L;
172 sce1->pqcoeffs[start+i] = R;
173 }
174 }
175 start += ics->swb_sizes[g];
176 }129 }
177 }130 }
131 if (sce->ics.predictor_reset_group) {
132 reset_predictor_group(sce, sce->ics.predictor_reset_group);
133 }
134 } else {
135 reset_all_predictors(sce->predictor_state);
178 }136 }
179}137}
180 138
181static inline void prepare_predictors(SingleChannelElement *sce)139/* If inc = 0 you can check if this returns 0 to see if you can reset freely */
182{
183 int k;
184 for (k = 0; k < MAX_PREDICTORS; k++)
185 predict(&sce->predictor_state[k], &sce->coeffs[k], &sce->prcoeffs[k], 0);
186}
187
188void ff_aac_update_main_pred(AACEncContext *s, SingleChannelElement *sce, ChannelElement *cpe)
189{
190 int k;
191
192 if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)
193 return;
194
195 if (cpe && cpe->common_window)
196 decode_joint_stereo(cpe);
197
198 for (k = 0; k < MAX_PREDICTORS; k++)
199 update_predictor(&sce->predictor_state[k], sce->pqcoeffs[k]);
200
201 if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
202 reset_all_predictors(sce);
203 }
204
205 if (sce->ics.predictor_reset_group)
206 reset_predictor_group(sce, sce->ics.predictor_reset_group);
207}
208
209/* If inc == 0 check if it returns 0 to see if you can reset freely */
210static inline int update_counters(IndividualChannelStream *ics, int inc)140static inline int update_counters(IndividualChannelStream *ics, int inc)
211{141{
212 int i, rg = 0;142 int i;
213 for (i = 1; i < 31; i++) {143 for (i = 1; i < 31; i++) {
214 ics->predictor_reset_count[i] += inc;144 ics->predictor_reset_count[i] += inc;
215 if (!rg && ics->predictor_reset_count[i] > PRED_RESET_FRAME_MIN)145 if (ics->predictor_reset_count[i] > PRED_RESET_FRAME_MIN)
216 rg = i; /* Reset this immediately */146 return i; /* Reset this immediately */
217 }147 }
218 return rg;148 return 0;
219}149}
220 150
221void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe)151void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe)
222{152{
223 int start, w, g, count = 0;153 int start, w, w2, g, i, count = 0;
224 SingleChannelElement *sce0 = &cpe->ch[0];154 SingleChannelElement *sce0 = &cpe->ch[0];
225 SingleChannelElement *sce1 = &cpe->ch[1];155 SingleChannelElement *sce1 = &cpe->ch[1];
156 const int pmax0 = FFMIN(sce0->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
157 const int pmax1 = FFMIN(sce1->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
158 const int pmax = FFMIN(pmax0, pmax1);
226 159
227 if (!cpe->common_window || sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)160 if (!cpe->common_window ||
161 sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
162 sce1->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)
228 return;163 return;
229 164
230 /* Predict if IS or MS is on and at least one channel is marked or when both are */
231 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {165 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
232 start = 0;166 start = 0;
233 for (g = 0; g < sce0->ics.num_swb; g++) {167 for (g = 0; g < sce0->ics.num_swb; g++) {
234 int sfb = w*16+g;168 int sfb = w*16+g;
235 if (sfb < PRED_SFB_START || sfb > ff_aac_pred_sfb_max[s->samplerate_index]) {169 int sum = sce0->ics.prediction_used[sfb] + sce1->ics.prediction_used[sfb];
236 ;170 float ener0 = 0.0f, ener1 = 0.0f, ener01 = 0.0f;
237 } else if ((cpe->is_mask[sfb] || cpe->ms_mask[sfb]) &&171 struct AACISError ph_err1, ph_err2, *erf;
238 (sce0->ics.prediction_used[sfb] || sce1->ics.prediction_used[sfb])) {172 if (sfb < PRED_SFB_START || sfb > pmax || sum != 2) {
239 sce0->ics.prediction_used[sfb] = sce1->ics.prediction_used[sfb] = 1;173 RESTORE_PRED(sce0, sfb);
240 count++;174 RESTORE_PRED(sce1, sfb);
241 } else if (sce0->ics.prediction_used[sfb] && sce1->ics.prediction_used[sfb]) {175 start += sce0->ics.swb_sizes[g];
176 continue;
177 }
178 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
179 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
180 float coef0 = sce0->pcoeffs[start+(w+w2)*128+i];
181 float coef1 = sce1->pcoeffs[start+(w+w2)*128+i];
182 ener0 += coef0*coef0;
183 ener1 += coef1*coef1;
184 ener01 += (coef0 + coef1)*(coef0 + coef1);
185 }
186 }
187 ph_err1 = ff_aac_is_encoding_err(s, cpe, start, w, g,
188 ener0, ener1, ener01, -1);
189 ph_err2 = ff_aac_is_encoding_err(s, cpe, start, w, g,
190 ener0, ener1, ener01, +1);
191 erf = ph_err1.error < ph_err2.error ? &ph_err1 : &ph_err2;
192 if (erf->pass) {
193 sce0->ics.prediction_used[sfb] = 1;
194 sce1->ics.prediction_used[sfb] = 1;
242 count++;195 count++;
243 } else {196 } else {
244 /* Restore band types, if changed - prediction never sets > RESERVED_BT */197 RESTORE_PRED(sce0, sfb);
245 if (sce0->ics.prediction_used[sfb] && sce0->band_type[sfb] < RESERVED_BT)198 RESTORE_PRED(sce1, sfb);
246 sce0->band_type[sfb] = sce0->orig_band_type[sfb];
247 if (sce1->ics.prediction_used[sfb] && sce1->band_type[sfb] < RESERVED_BT)
248 sce1->band_type[sfb] = sce1->orig_band_type[sfb];
249 sce0->ics.prediction_used[sfb] = sce1->ics.prediction_used[sfb] = 0;
250 }199 }
251 start += sce0->ics.swb_sizes[g];200 start += sce0->ics.swb_sizes[g];
252 }201 }
253 }202 }
254 203
255 sce1->ics.predictor_present = sce0->ics.predictor_present = !!count;204 sce1->ics.predictor_present = sce0->ics.predictor_present = !!count;
256
257 if (!count)
258 return;
259
260 sce1->ics.predictor_reset_group = sce0->ics.predictor_reset_group;
261}205}
262 206
263static void update_pred_resets(SingleChannelElement *sce)207static void update_pred_resets(SingleChannelElement *sce)
@@ -266,14 +210,12 @@ static void update_pred_resets(SingleChannelElement *sce) · 2 unmodified lines
266 float avg_frame = 0.0f;210 float avg_frame = 0.0f;
267 IndividualChannelStream *ics = &sce->ics;211 IndividualChannelStream *ics = &sce->ics;
268 212
269 /* Some other code probably chose the reset group */213 /* Update the counters and immediately update any frame behind schedule */
270 if (ics->predictor_reset_group)
271 return;
272
273 if ((ics->predictor_reset_group = update_counters(&sce->ics, 1)))214 if ((ics->predictor_reset_group = update_counters(&sce->ics, 1)))
274 return;215 return;
275 216
276 for (i = 1; i < 31; i++) {217 for (i = 1; i < 31; i++) {
218 /* Count-based */
277 if (ics->predictor_reset_count[i] > max_frame) {219 if (ics->predictor_reset_count[i] > max_frame) {
278 max_group_id_c = i;220 max_group_id_c = i;
279 max_frame = ics->predictor_reset_count[i];221 max_frame = ics->predictor_reset_count[i];
@@ -281,8 +223,7 @@ static void update_pred_resets(SingleChannelElement *sce) · 1 unmodified line
281 avg_frame = (ics->predictor_reset_count[i] + avg_frame)/2;223 avg_frame = (ics->predictor_reset_count[i] + avg_frame)/2;
282 }224 }
283 225
284 if (avg_frame*2 > max_frame && max_frame > PRED_RESET_MIN ||226 if (max_frame > PRED_RESET_MIN) {
285 max_frame > (2*PRED_RESET_MIN)/3) {
286 ics->predictor_reset_group = max_group_id_c;227 ics->predictor_reset_group = max_group_id_c;
287 } else {228 } else {
288 ics->predictor_reset_group = 0;229 ics->predictor_reset_group = 0;
@@ -291,56 +232,91 @@ static void update_pred_resets(SingleChannelElement *sce) · 2 unmodified lines
291 232
292void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce)233void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce)
293{234{
294 int sfb, i, count = 0;235 int sfb, i, count = 0, cost_coeffs = 0, cost_pred = 0;
295 float *O34 = &s->scoefs[256*0], *P34 = &s->scoefs[256*1];236 const int pmax = FFMIN(sce->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
296 int cost_coeffs = PRICE_OFFSET;237 float *O34 = &s->scoefs[128*0], *P34 = &s->scoefs[128*1];
297 int cost_pred = 1+(sce->ics.predictor_reset_group ? 5 : 0) +238 float *SENT = &s->scoefs[128*2], *S34 = &s->scoefs[128*3];
298 FFMIN(sce->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);239 float *QERR = &s->scoefs[128*4];
299 240
300 memcpy(sce->orig_band_type, sce->band_type, 128*sizeof(enum BandType));241 if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
242 sce->ics.predictor_present = 0;
243 return;
244 }
301 245
302 if (!sce->ics.predictor_initialized ||246 if (!sce->ics.predictor_initialized) {
303 sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE) {247 reset_all_predictors(sce->predictor_state);
304 reset_all_predictors(sce);248 sce->ics.predictor_initialized = 1;
249 memcpy(sce->prcoeffs, sce->coeffs, 1024*sizeof(float));
305 for (i = 1; i < 31; i++)250 for (i = 1; i < 31; i++)
306 sce->ics.predictor_reset_count[i] = i;251 sce->ics.predictor_reset_count[i] = i;
307 sce->ics.predictor_initialized = 1;
308 }252 }
309 253
310 update_pred_resets(sce);254 update_pred_resets(sce);
311 prepare_predictors(sce);255 memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
312 sce->ics.predictor_reset_group = 0;256
313 257 for (sfb = PRED_SFB_START; sfb < pmax; sfb++) {
314 for (sfb = PRED_SFB_START; sfb < ff_aac_pred_sfb_max[s->samplerate_index]; sfb++) {258 int cost1, cost2, cb_p;
315 float dist1 = 0.0f, dist2 = 0.0f;259 float dist1, dist2, dist_spec_err = 0.0f;
316 int swb_start = sce->ics.swb_offset[sfb];260 const int cb_n = sce->band_type[sfb];
317 int swb_len = sce->ics.swb_offset[sfb + 1] - swb_start;261 const int start_coef = sce->ics.swb_offset[sfb];
318 int cb1 = sce->band_type[sfb], cb2, bits1 = 0, bits2 = 0;262 const int num_coeffs = sce->ics.swb_offset[sfb + 1] - start_coef;
319 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[sfb];263 const FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[sfb];
320 abs_pow34_v(O34, &sce->coeffs[swb_start], swb_len);264
321 abs_pow34_v(P34, &sce->prcoeffs[swb_start], swb_len);265 if (start_coef + num_coeffs > MAX_PREDICTORS)
322 cb2 = find_min_book(find_max_val(1, swb_len, P34), sce->sf_idx[sfb]);266 continue;
323 if (cb2 <= cb1) {267
324 dist1 += quantize_band_cost(s, &sce->coeffs[swb_start], O34, swb_len,268 /* Normal coefficients */
325 sce->sf_idx[sfb], cb1, s->lambda / band->threshold,269 abs_pow34_v(O34, &sce->coeffs[start_coef], num_coeffs);
326 INFINITY, &bits1, 0);270 dist1 = quantize_and_encode_band_cost(s, NULL, &sce->coeffs[start_coef], NULL,
327 dist2 += quantize_band_cost(s, &sce->prcoeffs[swb_start], P34, swb_len,271 O34, num_coeffs, sce->sf_idx[sfb],
328 sce->sf_idx[sfb], cb2, s->lambda / band->threshold,272 cb_n, s->lambda / band->threshold, INFINITY, &cost1, 0);
329 INFINITY, &bits2, 0);273 cost_coeffs += cost1;
330 if (dist2 <= dist1) {274
331 sce->ics.prediction_used[sfb] = 1;275 /* Encoded coefficients - needed for #bits, band type and quant. error */
332 sce->band_type[sfb] = cb2;276 for (i = 0; i < num_coeffs; i++)
333 count++;277 SENT[i] = sce->coeffs[start_coef + i] - sce->prcoeffs[start_coef + i];
334 }278 abs_pow34_v(S34, SENT, num_coeffs);
335 cost_coeffs += bits1;279 if (cb_n < RESERVED_BT)
336 cost_pred += bits2;280 cb_p = find_min_book(find_max_val(1, num_coeffs, S34), sce->sf_idx[sfb]);
281 else
282 cb_p = cb_n;
283 quantize_and_encode_band_cost(s, NULL, SENT, QERR, S34, num_coeffs,
284 sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY,
285 &cost2, 0);
286
287 /* Reconstructed coefficients - needed for distortion measurements */
288 for (i = 0; i < num_coeffs; i++)
289 sce->prcoeffs[start_coef + i] += QERR[i] != 0.0f ? (sce->prcoeffs[start_coef + i] - QERR[i]) : 0.0f;
290 abs_pow34_v(P34, &sce->prcoeffs[start_coef], num_coeffs);
291 if (cb_n < RESERVED_BT)
292 cb_p = find_min_book(find_max_val(1, num_coeffs, P34), sce->sf_idx[sfb]);
293 else
294 cb_p = cb_n;
295 dist2 = quantize_and_encode_band_cost(s, NULL, &sce->prcoeffs[start_coef], NULL,
296 P34, num_coeffs, sce->sf_idx[sfb],
297 cb_p, s->lambda / band->threshold, INFINITY, NULL, 0);
298 for (i = 0; i < num_coeffs; i++)
299 dist_spec_err += (O34[i] - P34[i])*(O34[i] - P34[i]);
300 dist_spec_err *= s->lambda / band->threshold;
301 dist2 += dist_spec_err;
302
303 if (dist2 <= dist1 && cb_p <= cb_n) {
304 cost_pred += cost2;
305 sce->ics.prediction_used[sfb] = 1;
306 sce->band_alt[sfb] = cb_n;
307 sce->band_type[sfb] = cb_p;
308 count++;
309 } else {
310 cost_pred += cost1;
311 sce->band_alt[sfb] = cb_p;
337 }312 }
338 }313 }
339 314
340 if (count && cost_pred > cost_coeffs) {315 if (count && cost_coeffs < cost_pred) {
341 memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
342 memcpy(sce->band_type, sce->orig_band_type, sizeof(sce->band_type));
343 count = 0;316 count = 0;
317 for (sfb = PRED_SFB_START; sfb < pmax; sfb++)
318 RESTORE_PRED(sce, sfb);
319 memset(&sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
344 }320 }
345 321
346 sce->ics.predictor_present = !!count;322 sce->ics.predictor_present = !!count;
@@ -352,14 +328,15 @@ void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce) · 5 unmodified lines
352void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce)328void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce)
353{329{
354 int sfb;330 int sfb;
331 IndividualChannelStream *ics = &sce->ics;
332 const int pmax = FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
355 333
356 if (!sce->ics.predictor_present ||334 if (!ics->predictor_present)
357 sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)
358 return;335 return;
359 336
360 put_bits(&s->pb, 1, !!sce->ics.predictor_reset_group);337 put_bits(&s->pb, 1, !!ics->predictor_reset_group);
361 if (sce->ics.predictor_reset_group)338 if (ics->predictor_reset_group)
362 put_bits(&s->pb, 5, sce->ics.predictor_reset_group);339 put_bits(&s->pb, 5, ics->predictor_reset_group);
363 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]); sfb++)340 for (sfb = 0; sfb < pmax; sfb++)
364 put_bits(&s->pb, 1, sce->ics.prediction_used[sfb]);341 put_bits(&s->pb, 1, ics->prediction_used[sfb]);
365}342}
libavcodec/aacenc_pred.h+1−5
@@ -34,16 +34,12 @@ · 33 unmodified lines
34#define PRED_RESET_FRAME_MIN 24034#define PRED_RESET_FRAME_MIN 240
35 35
36/* Any frame with less than this amount of frames since last reset is ok */36/* Any frame with less than this amount of frames since last reset is ok */
37#define PRED_RESET_MIN 12837#define PRED_RESET_MIN 64
38 38
39/* Raise to filter any low frequency artifacts due to prediction */39/* Raise to filter any low frequency artifacts due to prediction */
40#define PRED_SFB_START 1040#define PRED_SFB_START 10
41 41
42/* Offset for the number of bits to encode normal coefficients */
43#define PRICE_OFFSET 440
44
45void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce);42void ff_aac_apply_main_pred(AACEncContext *s, SingleChannelElement *sce);
46void ff_aac_update_main_pred(AACEncContext *s, SingleChannelElement *sce, ChannelElement *cpe);
47void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe);43void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe);
48void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce);44void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce);
49void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce);45void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce);

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