Merge commit 'a1a143adb0fd11c474221431417cff25db7d920f'
[ffmpeg.git] / libavcodec / aacenc_pred.c
index fafe002..d111192 100644 (file)
@@ -21,7 +21,7 @@
 
 /**
  * @file
- * AAC encoder Intensity Stereo
+ * AAC encoder main-type prediction
  * @author Rostislav Pehlivanov ( atomnuker gmail com )
  */
 
@@ -148,7 +148,7 @@ static inline int update_counters(IndividualChannelStream *ics, int inc)
     return 0;
 }
 
-void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe)
+void ff_aac_adjust_common_pred(AACEncContext *s, ChannelElement *cpe)
 {
     int start, w, w2, g, i, count = 0;
     SingleChannelElement *sce0 = &cpe->ch[0];
@@ -179,15 +179,15 @@ void ff_aac_adjust_common_prediction(AACEncContext *s, ChannelElement *cpe)
                 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
                     float coef0 = sce0->pcoeffs[start+(w+w2)*128+i];
                     float coef1 = sce1->pcoeffs[start+(w+w2)*128+i];
-                    ener0 += coef0*coef0;
-                    ener1 += coef1*coef1;
+                    ener0  += coef0*coef0;
+                    ener1  += coef1*coef1;
                     ener01 += (coef0 + coef1)*(coef0 + coef1);
                 }
             }
             ph_err1 = ff_aac_is_encoding_err(s, cpe, start, w, g,
-                                             ener0, ener1, ener01, -1);
+                                             ener0, ener1, ener01, 1, -1);
             ph_err2 = ff_aac_is_encoding_err(s, cpe, start, w, g,
-                                             ener0, ener1, ener01, +1);
+                                             ener0, ener1, ener01, 1, +1);
             erf = ph_err1.error < ph_err2.error ? &ph_err1 : &ph_err2;
             if (erf->pass) {
                 sce0->ics.prediction_used[sfb] = 1;
@@ -257,44 +257,48 @@ void ff_aac_search_for_pred(AACEncContext *s, SingleChannelElement *sce)
     for (sfb = PRED_SFB_START; sfb < pmax; sfb++) {
         int cost1, cost2, cb_p;
         float dist1, dist2, dist_spec_err = 0.0f;
-        const int cb_n = sce->band_type[sfb];
+        const int cb_n = sce->zeroes[sfb] ? 0 : sce->band_type[sfb];
+        const int cb_min = sce->zeroes[sfb] ? 0 : 1;
+        const int cb_max = sce->zeroes[sfb] ? 0 : RESERVED_BT;
         const int start_coef = sce->ics.swb_offset[sfb];
         const int num_coeffs = sce->ics.swb_offset[sfb + 1] - start_coef;
         const FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[sfb];
 
-        if (start_coef + num_coeffs > MAX_PREDICTORS)
+        if (start_coef + num_coeffs > MAX_PREDICTORS ||
+            (s->cur_channel && sce->band_type[sfb] >= INTENSITY_BT2) ||
+            sce->band_type[sfb] == NOISE_BT)
             continue;
 
         /* Normal coefficients */
-        abs_pow34_v(O34, &sce->coeffs[start_coef], num_coeffs);
+        s->abs_pow34(O34, &sce->coeffs[start_coef], num_coeffs);
         dist1 = quantize_and_encode_band_cost(s, NULL, &sce->coeffs[start_coef], NULL,
                                               O34, num_coeffs, sce->sf_idx[sfb],
-                                              cb_n, s->lambda / band->threshold, INFINITY, &cost1, 0);
+                                              cb_n, s->lambda / band->threshold, INFINITY, &cost1, NULL, 0);
         cost_coeffs += cost1;
 
         /* Encoded coefficients - needed for #bits, band type and quant. error */
         for (i = 0; i < num_coeffs; i++)
             SENT[i] = sce->coeffs[start_coef + i] - sce->prcoeffs[start_coef + i];
-        abs_pow34_v(S34, SENT, num_coeffs);
+        s->abs_pow34(S34, SENT, num_coeffs);
         if (cb_n < RESERVED_BT)
-            cb_p = find_min_book(find_max_val(1, num_coeffs, S34), sce->sf_idx[sfb]);
+            cb_p = av_clip(find_min_book(find_max_val(1, num_coeffs, S34), sce->sf_idx[sfb]), cb_min, cb_max);
         else
             cb_p = cb_n;
         quantize_and_encode_band_cost(s, NULL, SENT, QERR, S34, num_coeffs,
                                       sce->sf_idx[sfb], cb_p, s->lambda / band->threshold, INFINITY,
-                                      &cost2, 0);
+                                      &cost2, NULL, 0);
 
         /* Reconstructed coefficients - needed for distortion measurements */
         for (i = 0; i < num_coeffs; i++)
             sce->prcoeffs[start_coef + i] += QERR[i] != 0.0f ? (sce->prcoeffs[start_coef + i] - QERR[i]) : 0.0f;
-        abs_pow34_v(P34, &sce->prcoeffs[start_coef], num_coeffs);
+        s->abs_pow34(P34, &sce->prcoeffs[start_coef], num_coeffs);
         if (cb_n < RESERVED_BT)
-            cb_p = find_min_book(find_max_val(1, num_coeffs, P34), sce->sf_idx[sfb]);
+            cb_p = av_clip(find_min_book(find_max_val(1, num_coeffs, P34), sce->sf_idx[sfb]), cb_min, cb_max);
         else
             cb_p = cb_n;
         dist2 = quantize_and_encode_band_cost(s, NULL, &sce->prcoeffs[start_coef], NULL,
                                               P34, num_coeffs, sce->sf_idx[sfb],
-                                              cb_p, s->lambda / band->threshold, INFINITY, NULL, 0);
+                                              cb_p, s->lambda / band->threshold, INFINITY, NULL, NULL, 0);
         for (i = 0; i < num_coeffs; i++)
             dist_spec_err += (O34[i] - P34[i])*(O34[i] - P34[i]);
         dist_spec_err *= s->lambda / band->threshold;
@@ -331,7 +335,8 @@ void ff_aac_encode_main_pred(AACEncContext *s, SingleChannelElement *sce)
     IndividualChannelStream *ics = &sce->ics;
     const int pmax = FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[s->samplerate_index]);
 
-    if (!ics->predictor_present)
+    if (s->profile != FF_PROFILE_AAC_MAIN ||
+        !ics->predictor_present)
         return;
 
     put_bits(&s->pb, 1, !!ics->predictor_reset_group);