Browse code

alac: reverse lpc coeff order, simplify filter

Reversing the lpc coefficient order simplifies indexing in the filter.

Justin Ruggles authored on 2012/07/20 03:08:22
Showing 1 changed files
... ...
@@ -194,6 +194,7 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
194 194
                            int lpc_order, int lpc_quant)
195 195
 {
196 196
     int i;
197
+    int32_t *pred = buffer_out;
197 198
 
198 199
     /* first sample always copies */
199 200
     *buffer_out = *error_buffer;
... ...
@@ -217,37 +218,35 @@ static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
217 217
     }
218 218
 
219 219
     /* read warm-up samples */
220
-    for (i = 0; i < lpc_order; i++) {
221
-        buffer_out[i + 1] = sign_extend(buffer_out[i] + error_buffer[i + 1],
222
-                                        bps);
223
-    }
220
+    for (i = 1; i <= lpc_order; i++)
221
+        buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
224 222
 
225 223
     /* NOTE: 4 and 8 are very common cases that could be optimized. */
226 224
 
227
-    for (i = lpc_order; i < nb_samples - 1; i++) {
225
+    for (; i < nb_samples; i++) {
228 226
         int j;
229 227
         int val = 0;
230
-        int error_val = error_buffer[i + 1];
228
+        int error_val = error_buffer[i];
231 229
         int error_sign;
232
-        int d = buffer_out[i - lpc_order];
230
+        int d = *pred++;
233 231
 
234 232
         /* LPC prediction */
235 233
         for (j = 0; j < lpc_order; j++)
236
-            val += (buffer_out[i - j] - d) * lpc_coefs[j];
234
+            val += (pred[j] - d) * lpc_coefs[j];
237 235
         val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
238 236
         val += d + error_val;
239
-        buffer_out[i + 1] = sign_extend(val, bps);
237
+        buffer_out[i] = sign_extend(val, bps);
240 238
 
241 239
         /* adapt LPC coefficients */
242 240
         error_sign = sign_only(error_val);
243 241
         if (error_sign) {
244
-            for (j = lpc_order - 1; j >= 0 && error_val * error_sign > 0; j--) {
242
+            for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
245 243
                 int sign;
246
-                val  = d - buffer_out[i - j];
244
+                val  = d - pred[j];
247 245
                 sign = sign_only(val) * error_sign;
248 246
                 lpc_coefs[j] -= sign;
249 247
                 val *= sign;
250
-                error_val -= (val >> lpc_quant) * (lpc_order - j);
248
+                error_val -= (val >> lpc_quant) * (j + 1);
251 249
             }
252 250
         }
253 251
     }
... ...
@@ -350,7 +349,7 @@ static int decode_element(AVCodecContext *avctx, void *data, int ch_index,
350 350
             lpc_order[ch]         = get_bits(&alac->gb, 5);
351 351
 
352 352
             /* read the predictor table */
353
-            for (i = 0; i < lpc_order[ch]; i++)
353
+            for (i = lpc_order[ch] - 1; i >= 0; i--)
354 354
                 lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
355 355
         }
356 356