lavc/aacsbr: fix make checkheaders warning
[ffmpeg.git] / libavcodec / opus_silk.c
index c64d24c..2fcbf3b 100644 (file)
@@ -128,8 +128,7 @@ static inline void silk_stabilize_lsf(int16_t nlsf[16], int order, const uint16_
     if (nlsf[0] < min_delta[0])
         nlsf[0] = min_delta[0];
     for (i = 1; i < order; i++)
-        if (nlsf[i] < nlsf[i - 1] + min_delta[i])
-            nlsf[i] = nlsf[i - 1] + min_delta[i];
+        nlsf[i] = FFMAX(nlsf[i], FFMIN(nlsf[i - 1] + min_delta[i], 32767));
 
     /* push backwards to increase distance */
     if (nlsf[order-1] > 32768 - min_delta[order])
@@ -186,8 +185,15 @@ static inline int silk_is_lpc_stable(const int16_t lpc[16], int order)
         row = lpc32[k & 1];
 
         for (j = 0; j < k; j++) {
-            int x = prevrow[j] - ROUND_MULL(prevrow[k - j - 1], rc, 31);
-            row[j] = ROUND_MULL(x, gain, fbits);
+            int x = av_sat_sub32(prevrow[j], ROUND_MULL(prevrow[k - j - 1], rc, 31));
+            int64_t tmp = ROUND_MULL(x, gain, fbits);
+
+            /* per RFC 8251 section 6, if this calculation overflows, the filter
+               is considered unstable. */
+            if (tmp < INT32_MIN || tmp > INT32_MAX)
+                return 0;
+
+            row[j] = (int32_t)tmp;
         }
     }
 }
@@ -233,8 +239,10 @@ static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)
 
     /* reconstruct A(z) */
     for (k = 0; k < order>>1; k++) {
-        lpc32[k]         = -p[k + 1] - p[k] - q[k + 1] + q[k];
-        lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k];
+        int32_t p_tmp = p[k + 1] + p[k];
+        int32_t q_tmp = q[k + 1] - q[k];
+        lpc32[k]         = -q_tmp - p_tmp;
+        lpc32[order-k-1] =  q_tmp - p_tmp;
     }
 
     /* limit the range of the LPC coefficients to each fit within an int16_t */
@@ -307,15 +315,15 @@ static inline void silk_decode_lpc(SilkContext *s, SilkFrame *frame,
     *lpc_order = order = s->wb ? 16 : 10;
 
     /* obtain LSF stage-1 and stage-2 indices */
-    lsf_i1 = opus_rc_getsymbol(rc, ff_silk_model_lsf_s1[s->wb][voiced]);
+    lsf_i1 = ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_s1[s->wb][voiced]);
     for (i = 0; i < order; i++) {
         int index = s->wb ? ff_silk_lsf_s2_model_sel_wb  [lsf_i1][i] :
                             ff_silk_lsf_s2_model_sel_nbmb[lsf_i1][i];
-        lsf_i2[i] = opus_rc_getsymbol(rc, ff_silk_model_lsf_s2[index]) - 4;
+        lsf_i2[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_s2[index]) - 4;
         if (lsf_i2[i] == -4)
-            lsf_i2[i] -= opus_rc_getsymbol(rc, ff_silk_model_lsf_s2_ext);
+            lsf_i2[i] -= ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_s2_ext);
         else if (lsf_i2[i] == 4)
-            lsf_i2[i] += opus_rc_getsymbol(rc, ff_silk_model_lsf_s2_ext);
+            lsf_i2[i] += ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_s2_ext);
     }
 
     /* reverse the backwards-prediction step */
@@ -365,7 +373,7 @@ static inline void silk_decode_lpc(SilkContext *s, SilkFrame *frame,
     /* and then convert both sets of NLSFs to LPC coefficients */
     *has_lpc_leadin = 0;
     if (s->subframes == 4) {
-        int offset = opus_rc_getsymbol(rc, ff_silk_model_lsf_interpolation_offset);
+        int offset = ff_opus_rc_dec_cdf(rc, ff_silk_model_lsf_interpolation_offset);
         if (offset != 4 && frame->coded) {
             *has_lpc_leadin = 1;
             if (offset != 0) {
@@ -394,7 +402,7 @@ static inline void silk_count_children(OpusRangeCoder *rc, int model, int32_t to
                                        int32_t child[2])
 {
     if (total != 0) {
-        child[0] = opus_rc_getsymbol(rc,
+        child[0] = ff_opus_rc_dec_cdf(rc,
                        ff_silk_model_pulse_location[model] + (((total - 1 + 5) * (total - 1)) >> 1));
         child[1] = total - child[0];
     } else {
@@ -416,17 +424,17 @@ static inline void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc,
     int32_t excitation[320];    // Q23
 
     /* excitation parameters */
-    seed = opus_rc_getsymbol(rc, ff_silk_model_lcg_seed);
+    seed = ff_opus_rc_dec_cdf(rc, ff_silk_model_lcg_seed);
     shellblocks = ff_silk_shell_blocks[s->bandwidth][s->subframes >> 2];
-    ratelevel = opus_rc_getsymbol(rc, ff_silk_model_exc_rate[voiced]);
+    ratelevel = ff_opus_rc_dec_cdf(rc, ff_silk_model_exc_rate[voiced]);
 
     for (i = 0; i < shellblocks; i++) {
-        pulsecount[i] = opus_rc_getsymbol(rc, ff_silk_model_pulse_count[ratelevel]);
+        pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[ratelevel]);
         if (pulsecount[i] == 17) {
             while (pulsecount[i] == 17 && ++lsbcount[i] != 10)
-                pulsecount[i] = opus_rc_getsymbol(rc, ff_silk_model_pulse_count[9]);
+                pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[9]);
             if (lsbcount[i] == 10)
-                pulsecount[i] = opus_rc_getsymbol(rc, ff_silk_model_pulse_count[10]);
+                pulsecount[i] = ff_opus_rc_dec_cdf(rc, ff_silk_model_pulse_count[10]);
         }
     }
 
@@ -461,13 +469,13 @@ static inline void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc,
         int bit;
         for (bit = 0; bit < lsbcount[i >> 4]; bit++)
             excitation[i] = (excitation[i] << 1) |
-                            opus_rc_getsymbol(rc, ff_silk_model_excitation_lsb);
+                            ff_opus_rc_dec_cdf(rc, ff_silk_model_excitation_lsb);
     }
 
     /* decode signs */
     for (i = 0; i < shellblocks << 4; i++) {
         if (excitation[i] != 0) {
-            int sign = opus_rc_getsymbol(rc, ff_silk_model_excitation_sign[active +
+            int sign = ff_opus_rc_dec_cdf(rc, ff_silk_model_excitation_sign[active +
                                          voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)]);
             if (sign == 0)
                 excitation[i] *= -1;
@@ -522,11 +530,11 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
     /* obtain stereo weights */
     if (coded_channels == 2 && channel == 0) {
         int n, wi[2], ws[2], w[2];
-        n     = opus_rc_getsymbol(rc, ff_silk_model_stereo_s1);
-        wi[0] = opus_rc_getsymbol(rc, ff_silk_model_stereo_s2) + 3 * (n / 5);
-        ws[0] = opus_rc_getsymbol(rc, ff_silk_model_stereo_s3);
-        wi[1] = opus_rc_getsymbol(rc, ff_silk_model_stereo_s2) + 3 * (n % 5);
-        ws[1] = opus_rc_getsymbol(rc, ff_silk_model_stereo_s3);
+        n     = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s1);
+        wi[0] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s2) + 3 * (n / 5);
+        ws[0] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s3);
+        wi[1] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s2) + 3 * (n % 5);
+        ws[1] = ff_opus_rc_dec_cdf(rc, ff_silk_model_stereo_s3);
 
         for (i = 0; i < 2; i++)
             w[i] = ff_silk_stereo_weights[wi[i]] +
@@ -537,15 +545,15 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
         s->stereo_weights[1] = w[1]          / 8192.0;
 
         /* and read the mid-only flag */
-        s->midonly = active1 ? 0 : opus_rc_getsymbol(rc, ff_silk_model_mid_only);
+        s->midonly = active1 ? 0 : ff_opus_rc_dec_cdf(rc, ff_silk_model_mid_only);
     }
 
     /* obtain frame type */
     if (!active) {
-        qoffset_high = opus_rc_getsymbol(rc, ff_silk_model_frame_type_inactive);
+        qoffset_high = ff_opus_rc_dec_cdf(rc, ff_silk_model_frame_type_inactive);
         voiced = 0;
     } else {
-        int type = opus_rc_getsymbol(rc, ff_silk_model_frame_type_active);
+        int type = ff_opus_rc_dec_cdf(rc, ff_silk_model_frame_type_active);
         qoffset_high = type & 1;
         voiced = type >> 1;
     }
@@ -557,14 +565,14 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
 
         if (i == 0 && (frame_num == 0 || !frame->coded)) {
             /* gain is coded absolute */
-            int x = opus_rc_getsymbol(rc, ff_silk_model_gain_highbits[active + voiced]);
-            log_gain = (x<<3) | opus_rc_getsymbol(rc, ff_silk_model_gain_lowbits);
+            int x = ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_highbits[active + voiced]);
+            log_gain = (x<<3) | ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_lowbits);
 
             if (frame->coded)
                 log_gain = FFMAX(log_gain, frame->log_gain - 16);
         } else {
             /* gain is coded relative */
-            int delta_gain = opus_rc_getsymbol(rc, ff_silk_model_gain_delta);
+            int delta_gain = ff_opus_rc_dec_cdf(rc, ff_silk_model_gain_delta);
             log_gain = av_clip_uintp2(FFMAX((delta_gain<<1) - 16,
                                      frame->log_gain + delta_gain - 4), 6);
         }
@@ -590,7 +598,7 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
         const int8_t * offsets;
 
         if (!lag_absolute) {
-            int delta = opus_rc_getsymbol(rc, ff_silk_model_pitch_delta);
+            int delta = ff_opus_rc_dec_cdf(rc, ff_silk_model_pitch_delta);
             if (delta)
                 primarylag = frame->primarylag + delta - 9;
             else
@@ -600,12 +608,12 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
         if (lag_absolute) {
             /* primary lag is coded absolute */
             int highbits, lowbits;
-            static const uint16_t *model[] = {
+            static const uint16_t * const model[] = {
                 ff_silk_model_pitch_lowbits_nb, ff_silk_model_pitch_lowbits_mb,
                 ff_silk_model_pitch_lowbits_wb
             };
-            highbits = opus_rc_getsymbol(rc, ff_silk_model_pitch_highbits);
-            lowbits  = opus_rc_getsymbol(rc, model[s->bandwidth]);
+            highbits = ff_opus_rc_dec_cdf(rc, ff_silk_model_pitch_highbits);
+            lowbits  = ff_opus_rc_dec_cdf(rc, model[s->bandwidth]);
 
             primarylag = ff_silk_pitch_min_lag[s->bandwidth] +
                          highbits*ff_silk_pitch_scale[s->bandwidth] + lowbits;
@@ -614,15 +622,15 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
 
         if (s->subframes == 2)
             offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
-                     ? ff_silk_pitch_offset_nb10ms[opus_rc_getsymbol(rc,
+                     ? ff_silk_pitch_offset_nb10ms[ff_opus_rc_dec_cdf(rc,
                                                 ff_silk_model_pitch_contour_nb10ms)]
-                     : ff_silk_pitch_offset_mbwb10ms[opus_rc_getsymbol(rc,
+                     : ff_silk_pitch_offset_mbwb10ms[ff_opus_rc_dec_cdf(rc,
                                                 ff_silk_model_pitch_contour_mbwb10ms)];
         else
             offsets = (s->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
-                     ? ff_silk_pitch_offset_nb20ms[opus_rc_getsymbol(rc,
+                     ? ff_silk_pitch_offset_nb20ms[ff_opus_rc_dec_cdf(rc,
                                                 ff_silk_model_pitch_contour_nb20ms)]
-                     : ff_silk_pitch_offset_mbwb20ms[opus_rc_getsymbol(rc,
+                     : ff_silk_pitch_offset_mbwb20ms[ff_opus_rc_dec_cdf(rc,
                                                 ff_silk_model_pitch_contour_mbwb20ms)];
 
         for (i = 0; i < s->subframes; i++)
@@ -631,17 +639,17 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
                                      ff_silk_pitch_max_lag[s->bandwidth]);
 
         /* obtain LTP filter coefficients */
-        ltpfilter = opus_rc_getsymbol(rc, ff_silk_model_ltp_filter);
+        ltpfilter = ff_opus_rc_dec_cdf(rc, ff_silk_model_ltp_filter);
         for (i = 0; i < s->subframes; i++) {
             int index, j;
-            static const uint16_t *filter_sel[] = {
+            static const uint16_t * const filter_sel[] = {
                 ff_silk_model_ltp_filter0_sel, ff_silk_model_ltp_filter1_sel,
                 ff_silk_model_ltp_filter2_sel
             };
-            static const int8_t (*filter_taps[])[5] = {
+            static const int8_t (* const filter_taps[])[5] = {
                 ff_silk_ltp_filter0_taps, ff_silk_ltp_filter1_taps, ff_silk_ltp_filter2_taps
             };
-            index = opus_rc_getsymbol(rc, filter_sel[ltpfilter]);
+            index = ff_opus_rc_dec_cdf(rc, filter_sel[ltpfilter]);
             for (j = 0; j < 5; j++)
                 sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f;
         }
@@ -649,7 +657,7 @@ static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc,
 
     /* obtain LTP scale factor */
     if (voiced && frame_num == 0)
-        ltpscale = ff_silk_ltp_scale_factor[opus_rc_getsymbol(rc,
+        ltpscale = ff_silk_ltp_scale_factor[ff_opus_rc_dec_cdf(rc,
                                          ff_silk_model_ltp_scale_index)] / 16384.0f;
     else ltpscale = 15565.0f/16384.0f;
 
@@ -803,11 +811,11 @@ int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc,
     /* read the LP-layer header bits */
     for (i = 0; i < coded_channels; i++) {
         for (j = 0; j < nb_frames; j++)
-            active[i][j] = opus_rc_p2model(rc, 1);
+            active[i][j] = ff_opus_rc_dec_log(rc, 1);
 
-        redundancy[i] = opus_rc_p2model(rc, 1);
+        redundancy[i] = ff_opus_rc_dec_log(rc, 1);
         if (redundancy[i]) {
-            av_log(s->avctx, AV_LOG_ERROR, "LBRR frames present; this is unsupported\n");
+            avpriv_report_missing_feature(s->avctx, "LBRR frames");
             return AVERROR_PATCHWELCOME;
         }
     }