Revision a403fc03 libavcodec/flacenc.c

View differences:

libavcodec/flacenc.c
37 37
#define FLAC_CHMODE_RIGHT_SIDE      9
38 38
#define FLAC_CHMODE_MID_SIDE       10
39 39

  
40
#define ORDER_METHOD_EST     0
41
#define ORDER_METHOD_2LEVEL  1
42
#define ORDER_METHOD_4LEVEL  2
43
#define ORDER_METHOD_8LEVEL  3
44
#define ORDER_METHOD_SEARCH  4
45

  
40 46
#define FLAC_STREAMINFO_SIZE  34
41 47

  
48
#define MIN_LPC_ORDER       1
49
#define MAX_LPC_ORDER      32
50
#define MAX_FIXED_ORDER     4
51
#define MAX_PARTITION_ORDER 8
52
#define MAX_PARTITIONS     (1 << MAX_PARTITION_ORDER)
53
#define MAX_LPC_PRECISION  15
54
#define MAX_LPC_SHIFT      15
55
#define MAX_RICE_PARAM     14
56

  
57
typedef struct CompressionOptions {
58
    int compression_level;
59
    int block_time_ms;
60
    int use_lpc;
61
    int lpc_coeff_precision;
62
    int min_prediction_order;
63
    int max_prediction_order;
64
    int prediction_order_method;
65
    int min_partition_order;
66
    int max_partition_order;
67
} CompressionOptions;
68

  
42 69
typedef struct RiceContext {
43 70
    int porder;
44
    int params[256];
71
    int params[MAX_PARTITIONS];
45 72
} RiceContext;
46 73

  
47 74
typedef struct FlacSubframe {
......
49 76
    int type_code;
50 77
    int obits;
51 78
    int order;
79
    int32_t coefs[MAX_LPC_ORDER];
80
    int shift;
52 81
    RiceContext rc;
53 82
    int32_t samples[FLAC_MAX_BLOCKSIZE];
54 83
    int32_t residual[FLAC_MAX_BLOCKSIZE];
......
72 101
    int max_framesize;
73 102
    uint32_t frame_count;
74 103
    FlacFrame frame;
104
    CompressionOptions options;
75 105
    AVCodecContext *avctx;
76 106
} FlacEncodeContext;
77 107

  
......
112 142
    /* MD5 signature = 0 */
113 143
}
114 144

  
115
#define BLOCK_TIME_MS 27
116

  
117 145
/**
118 146
 * Sets blocksize based on samplerate
119 147
 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
120 148
 */
121
static int select_blocksize(int samplerate)
149
static int select_blocksize(int samplerate, int block_time_ms)
122 150
{
123 151
    int i;
124 152
    int target;
......
126 154

  
127 155
    assert(samplerate > 0);
128 156
    blocksize = flac_blocksizes[1];
129
    target = (samplerate * BLOCK_TIME_MS) / 1000;
157
    target = (samplerate * block_time_ms) / 1000;
130 158
    for(i=0; i<16; i++) {
131 159
        if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
132 160
            blocksize = flac_blocksizes[i];
......
183 211
        s->samplerate = freq;
184 212
    }
185 213

  
186
    s->blocksize = select_blocksize(s->samplerate);
187
    avctx->frame_size = s->blocksize;
214
    /* set compression option defaults based on avctx->compression_level */
215
    if(avctx->compression_level < 0) {
216
        s->options.compression_level = 5;
217
    } else {
218
        s->options.compression_level = avctx->compression_level;
219
    }
220
    av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level);
221

  
222
    if(s->options.compression_level == 0) {
223
        s->options.block_time_ms = 27;
224
        s->options.use_lpc = 0;
225
        s->options.min_prediction_order = 2;
226
        s->options.max_prediction_order = 3;
227
        s->options.prediction_order_method = ORDER_METHOD_EST;
228
        s->options.min_partition_order = 2;
229
        s->options.max_partition_order = 2;
230
    } else if(s->options.compression_level == 1) {
231
        s->options.block_time_ms = 27;
232
        s->options.use_lpc = 0;
233
        s->options.min_prediction_order = 0;
234
        s->options.max_prediction_order = 4;
235
        s->options.prediction_order_method = ORDER_METHOD_EST;
236
        s->options.min_partition_order = 2;
237
        s->options.max_partition_order = 2;
238
    } else if(s->options.compression_level == 2) {
239
        s->options.block_time_ms = 27;
240
        s->options.use_lpc = 0;
241
        s->options.min_prediction_order = 0;
242
        s->options.max_prediction_order = 4;
243
        s->options.prediction_order_method = ORDER_METHOD_EST;
244
        s->options.min_partition_order = 0;
245
        s->options.max_partition_order = 3;
246
    } else if(s->options.compression_level == 3) {
247
        s->options.block_time_ms = 105;
248
        s->options.use_lpc = 1;
249
        s->options.min_prediction_order = 1;
250
        s->options.max_prediction_order = 6;
251
        s->options.prediction_order_method = ORDER_METHOD_EST;
252
        s->options.min_partition_order = 0;
253
        s->options.max_partition_order = 3;
254
    } else if(s->options.compression_level == 4) {
255
        s->options.block_time_ms = 105;
256
        s->options.use_lpc = 1;
257
        s->options.min_prediction_order = 1;
258
        s->options.max_prediction_order = 8;
259
        s->options.prediction_order_method = ORDER_METHOD_EST;
260
        s->options.min_partition_order = 0;
261
        s->options.max_partition_order = 3;
262
    } else if(s->options.compression_level == 5) {
263
        s->options.block_time_ms = 105;
264
        s->options.use_lpc = 1;
265
        s->options.min_prediction_order = 1;
266
        s->options.max_prediction_order = 8;
267
        s->options.prediction_order_method = ORDER_METHOD_EST;
268
        s->options.min_partition_order = 0;
269
        s->options.max_partition_order = 8;
270
    } else {
271
        av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
272
               s->options.compression_level);
273
        return -1;
274
    }
275

  
276
    /* set compression option overrides from AVCodecContext */
277
    if(avctx->use_lpc >= 0) {
278
        s->options.use_lpc = !!avctx->use_lpc;
279
    }
280
    av_log(avctx, AV_LOG_DEBUG, " use lpc: %s\n",
281
           s->options.use_lpc? "yes" : "no");
282

  
283
    if(avctx->min_prediction_order >= 0) {
284
        if(s->options.use_lpc) {
285
            if(avctx->min_prediction_order < MIN_LPC_ORDER ||
286
                    avctx->min_prediction_order > MAX_LPC_ORDER) {
287
                av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
288
                       avctx->min_prediction_order);
289
                return -1;
290
            }
291
        } else {
292
            if(avctx->min_prediction_order > MAX_FIXED_ORDER) {
293
                av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
294
                       avctx->min_prediction_order);
295
                return -1;
296
            }
297
        }
298
        s->options.min_prediction_order = avctx->min_prediction_order;
299
    }
300
    if(avctx->max_prediction_order >= 0) {
301
        if(s->options.use_lpc) {
302
            if(avctx->max_prediction_order < MIN_LPC_ORDER ||
303
                    avctx->max_prediction_order > MAX_LPC_ORDER) {
304
                av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
305
                       avctx->max_prediction_order);
306
                return -1;
307
            }
308
        } else {
309
            if(avctx->max_prediction_order > MAX_FIXED_ORDER) {
310
                av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
311
                       avctx->max_prediction_order);
312
                return -1;
313
            }
314
        }
315
        s->options.max_prediction_order = avctx->max_prediction_order;
316
    }
317
    if(s->options.max_prediction_order < s->options.min_prediction_order) {
318
        av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
319
               s->options.min_prediction_order, s->options.max_prediction_order);
320
        return -1;
321
    }
322
    av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
323
           s->options.min_prediction_order, s->options.max_prediction_order);
324

  
325
    if(avctx->prediction_order_method >= 0) {
326
        if(avctx->prediction_order_method > ORDER_METHOD_SEARCH) {
327
            av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
328
                   avctx->prediction_order_method);
329
            return -1;
330
        }
331
        s->options.prediction_order_method = avctx->prediction_order_method;
332
    }
333
    switch(avctx->prediction_order_method) {
334
        case ORDER_METHOD_EST:    av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
335
                                         "estimate"); break;
336
        case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
337
                                         "2-level"); break;
338
        case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
339
                                         "4-level"); break;
340
        case ORDER_METHOD_8LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
341
                                         "8-level"); break;
342
        case ORDER_METHOD_SEARCH: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
343
                                         "full search"); break;
344
    }
345

  
346
    if(avctx->min_partition_order >= 0) {
347
        if(avctx->min_partition_order > MAX_PARTITION_ORDER) {
348
            av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
349
                   avctx->min_partition_order);
350
            return -1;
351
        }
352
        s->options.min_partition_order = avctx->min_partition_order;
353
    }
354
    if(avctx->max_partition_order >= 0) {
355
        if(avctx->max_partition_order > MAX_PARTITION_ORDER) {
356
            av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
357
                   avctx->max_partition_order);
358
            return -1;
359
        }
360
        s->options.max_partition_order = avctx->max_partition_order;
361
    }
362
    if(s->options.max_partition_order < s->options.min_partition_order) {
363
        av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
364
               s->options.min_partition_order, s->options.max_partition_order);
365
        return -1;
366
    }
367
    av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
368
           s->options.min_partition_order, s->options.max_partition_order);
369

  
370
    if(avctx->frame_size > 0) {
371
        if(avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
372
                avctx->frame_size > FLAC_MIN_BLOCKSIZE) {
373
            av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
374
                   avctx->frame_size);
375
            return -1;
376
        }
377
        s->blocksize = avctx->frame_size;
378
    } else {
379
        s->blocksize = select_blocksize(s->samplerate, s->options.block_time_ms);
380
        avctx->frame_size = s->blocksize;
381
    }
382
    av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->blocksize);
383

  
384
    /* set LPC precision */
385
    if(avctx->lpc_coeff_precision > 0) {
386
        if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
387
            av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
388
                   avctx->lpc_coeff_precision);
389
            return -1;
390
        }
391
        s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
392
    } else {
393
        /* select LPC precision based on block size */
394
        if(     s->blocksize <=   192) s->options.lpc_coeff_precision =  7;
395
        else if(s->blocksize <=   384) s->options.lpc_coeff_precision =  8;
396
        else if(s->blocksize <=   576) s->options.lpc_coeff_precision =  9;
397
        else if(s->blocksize <=  1152) s->options.lpc_coeff_precision = 10;
398
        else if(s->blocksize <=  2304) s->options.lpc_coeff_precision = 11;
399
        else if(s->blocksize <=  4608) s->options.lpc_coeff_precision = 12;
400
        else if(s->blocksize <=  8192) s->options.lpc_coeff_precision = 13;
401
        else if(s->blocksize <= 16384) s->options.lpc_coeff_precision = 14;
402
        else                           s->options.lpc_coeff_precision = 15;
403
    }
404
    av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
405
           s->options.lpc_coeff_precision);
188 406

  
189 407
    /* set maximum encoded frame size in verbatim mode */
190 408
    if(s->channels == 2) {
......
259 477
static int find_optimal_param(uint32_t sum, int n)
260 478
{
261 479
    int k, k_opt;
262
    uint32_t nbits, nbits_opt;
480
    uint32_t nbits[MAX_RICE_PARAM+1];
263 481

  
264 482
    k_opt = 0;
265
    nbits_opt = rice_encode_count(sum, n, 0);
266
    for(k=1; k<=14; k++) {
267
        nbits = rice_encode_count(sum, n, k);
268
        if(nbits < nbits_opt) {
269
            nbits_opt = nbits;
483
    nbits[0] = UINT32_MAX;
484
    for(k=0; k<=MAX_RICE_PARAM; k++) {
485
        nbits[k] = rice_encode_count(sum, n, k);
486
        if(nbits[k] < nbits[k_opt]) {
270 487
            k_opt = k;
271 488
        }
272 489
    }
......
297 514
    return all_bits;
298 515
}
299 516

  
300
static void calc_sums(int pmax, uint32_t *data, int n, int pred_order,
301
                      uint32_t sums[][256])
517
static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
518
                      uint32_t sums[][MAX_PARTITIONS])
302 519
{
303 520
    int i, j;
304 521
    int parts;
......
316 533
        res_end+= n >> pmax;
317 534
    }
318 535
    /* sums for lower levels */
319
    for(i=pmax-1; i>=0; i--) {
536
    for(i=pmax-1; i>=pmin; i--) {
320 537
        parts = (1 << i);
321 538
        for(j=0; j<parts; j++) {
322 539
            sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
......
324 541
    }
325 542
}
326 543

  
327
static uint32_t calc_rice_params(RiceContext *rc, int pmax, int32_t *data,
328
                                 int n, int pred_order)
544
static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
545
                                 int32_t *data, int n, int pred_order)
329 546
{
330 547
    int i;
331
    uint32_t bits, opt_bits;
548
    uint32_t bits[MAX_PARTITION_ORDER+1];
332 549
    int opt_porder;
333
    RiceContext opt_rc;
550
    RiceContext tmp_rc;
334 551
    uint32_t *udata;
335
    uint32_t sums[9][256];
552
    uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
336 553

  
337
    assert(pmax >= 0 && pmax <= 8);
554
    assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
555
    assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
556
    assert(pmin <= pmax);
338 557

  
339 558
    udata = av_malloc(n * sizeof(uint32_t));
340 559
    for(i=0; i<n; i++) {
341 560
        udata[i] = (2*data[i]) ^ (data[i]>>31);
342 561
    }
343 562

  
344
    calc_sums(pmax, udata, n, pred_order, sums);
563
    calc_sums(pmin, pmax, udata, n, pred_order, sums);
345 564

  
346
    opt_porder = 0;
347
    opt_bits = UINT32_MAX;
348
    for(i=0; i<=pmax; i++) {
349
        bits = calc_optimal_rice_params(rc, i, sums[i], n, pred_order);
350
        if(bits < opt_bits) {
351
            opt_bits = bits;
565
    opt_porder = pmin;
566
    bits[pmin] = UINT32_MAX;
567
    for(i=pmin; i<=pmax; i++) {
568
        bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
569
        if(bits[i] <= bits[opt_porder]) {
352 570
            opt_porder = i;
353
            memcpy(&opt_rc, rc, sizeof(RiceContext));
571
            memcpy(rc, &tmp_rc, sizeof(RiceContext));
354 572
        }
355 573
    }
356
    if(opt_porder != pmax) {
357
        memcpy(rc, &opt_rc, sizeof(RiceContext));
358
    }
359 574

  
360 575
    av_freep(&udata);
361
    return opt_bits;
576
    return bits[opt_porder];
362 577
}
363 578

  
364
static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmax, int32_t *data,
365
                                       int n, int pred_order, int bps)
579
static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
580
                                       int32_t *data, int n, int pred_order,
581
                                       int bps)
366 582
{
367 583
    uint32_t bits;
368 584
    bits = pred_order*bps + 6;
369
    bits += calc_rice_params(rc, pmax, data, n, pred_order);
585
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
586
    return bits;
587
}
588

  
589
static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
590
                                     int32_t *data, int n, int pred_order,
591
                                     int bps, int precision)
592
{
593
    uint32_t bits;
594
    bits = pred_order*bps + 4 + 5 + pred_order*precision + 6;
595
    bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
370 596
    return bits;
371 597
}
372 598

  
599
/**
600
 * Apply Welch window function to audio block
601
 */
602
static void apply_welch_window(const int32_t *data, int len, double *w_data)
603
{
604
    int i, n2;
605
    double w;
606
    double c;
607

  
608
    n2 = (len >> 1);
609
    c = 2.0 / (len - 1.0);
610
    for(i=0; i<n2; i++) {
611
        w = c - i - 1.0;
612
        w = 1.0 - (w * w);
613
        w_data[i] = data[i] * w;
614
        w_data[len-1-i] = data[len-1-i] * w;
615
    }
616
}
617

  
618
/**
619
 * Calculates autocorrelation data from audio samples
620
 * A Welch window function is applied before calculation.
621
 */
622
static void compute_autocorr(const int32_t *data, int len, int lag,
623
                             double *autoc)
624
{
625
    int i;
626
    double *data1;
627
    int lag_ptr, ptr;
628

  
629
    data1 = av_malloc(len * sizeof(double));
630
    apply_welch_window(data, len, data1);
631

  
632
    for(i=0; i<lag; i++) autoc[i] = 1.0;
633

  
634
    ptr = 0;
635
    while(ptr <= lag) {
636
        lag_ptr = 0;
637
        while(lag_ptr <= ptr) {
638
            autoc[ptr-lag_ptr] += data1[ptr] * data1[lag_ptr];
639
            lag_ptr++;
640
        }
641
        ptr++;
642
    }
643
    while(ptr < len) {
644
        lag_ptr = ptr - lag;
645
        while(lag_ptr <= ptr) {
646
            autoc[ptr-lag_ptr] += data1[ptr] * data1[lag_ptr];
647
            lag_ptr++;
648
        }
649
        ptr++;
650
    }
651

  
652
    av_freep(&data1);
653
}
654

  
655
/**
656
 * Levinson-Durbin recursion.
657
 * Produces LPC coefficients from autocorrelation data.
658
 */
659
static void compute_lpc_coefs(const double *autoc, int max_order,
660
                              double lpc[][MAX_LPC_ORDER], double *ref)
661
{
662
   int i, j, i2;
663
   double r, err, tmp;
664
   double lpc_tmp[MAX_LPC_ORDER];
665

  
666
   for(i=0; i<max_order; i++) lpc_tmp[i] = 0;
667
   err = autoc[0];
668

  
669
   for(i=0; i<max_order; i++) {
670
      r = -autoc[i+1];
671
      for(j=0; j<i; j++) {
672
          r -= lpc_tmp[j] * autoc[i-j];
673
      }
674
      r /= err;
675
      ref[i] = fabs(r);
676

  
677
      err *= 1.0 - (r * r);
678

  
679
      i2 = (i >> 1);
680
      lpc_tmp[i] = r;
681
      for(j=0; j<i2; j++) {
682
         tmp = lpc_tmp[j];
683
         lpc_tmp[j] += r * lpc_tmp[i-1-j];
684
         lpc_tmp[i-1-j] += r * tmp;
685
      }
686
      if(i & 1) {
687
          lpc_tmp[j] += lpc_tmp[j] * r;
688
      }
689

  
690
      for(j=0; j<=i; j++) {
691
          lpc[i][j] = -lpc_tmp[j];
692
      }
693
   }
694
}
695

  
696
/**
697
 * Quantize LPC coefficients
698
 */
699
static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
700
                               int32_t *lpc_out, int *shift)
701
{
702
    int i;
703
    double d, cmax;
704
    int32_t qmax;
705
    int sh;
706

  
707
    /* define maximum levels */
708
    qmax = (1 << (precision - 1)) - 1;
709

  
710
    /* find maximum coefficient value */
711
    cmax = 0.0;
712
    for(i=0; i<order; i++) {
713
        d = lpc_in[i];
714
        if(d < 0) d = -d;
715
        if(d > cmax)
716
            cmax = d;
717
    }
718

  
719
    /* if maximum value quantizes to zero, return all zeros */
720
    if(cmax * (1 << MAX_LPC_SHIFT) < 1.0) {
721
        *shift = 0;
722
        for(i=0; i<order; i++) {
723
            lpc_out[i] = 0;
724
        }
725
        return;
726
    }
727

  
728
    /* calculate level shift which scales max coeff to available bits */
729
    sh = MAX_LPC_SHIFT;
730
    while((cmax * (1 << sh) > qmax) && (sh > 0)) {
731
        sh--;
732
    }
733

  
734
    /* since negative shift values are unsupported in decoder, scale down
735
       coefficients instead */
736
    if(sh == 0 && cmax > qmax) {
737
        double scale = ((double)qmax) / cmax;
738
        for(i=0; i<order; i++) {
739
            lpc_in[i] *= scale;
740
        }
741
    }
742

  
743
    /* output quantized coefficients and level shift */
744
    for(i=0; i<order; i++) {
745
        lpc_out[i] = (int32_t)(lpc_in[i] * (1 << sh));
746
    }
747
    *shift = sh;
748
}
749

  
750
static int estimate_best_order(double *ref, int max_order)
751
{
752
    int i, est;
753

  
754
    est = 1;
755
    for(i=max_order-1; i>=0; i--) {
756
        if(ref[i] > 0.10) {
757
            est = i+1;
758
            break;
759
        }
760
    }
761
    return est;
762
}
763

  
764
/**
765
 * Calculate LPC coefficients for multiple orders
766
 */
767
static int lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order,
768
                          int precision, int32_t coefs[][MAX_LPC_ORDER],
769
                          int *shift)
770
{
771
    double autoc[MAX_LPC_ORDER+1];
772
    double ref[MAX_LPC_ORDER];
773
    double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
774
    int i;
775
    int opt_order;
776

  
777
    assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
778

  
779
    compute_autocorr(samples, blocksize, max_order+1, autoc);
780

  
781
    compute_lpc_coefs(autoc, max_order, lpc, ref);
782

  
783
    opt_order = estimate_best_order(ref, max_order);
784

  
785
    i = opt_order-1;
786
    quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
787

  
788
    return opt_order;
789
}
790

  
791

  
373 792
static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
374 793
{
375 794
    assert(n > 0);
376 795
    memcpy(res, smp, n * sizeof(int32_t));
377 796
}
378 797

  
379
static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
798
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
799
                                  int order)
380 800
{
381 801
    int i;
382 802

  
......
402 822
    }
403 823
}
404 824

  
825
static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
826
                                int order, const int32_t *coefs, int shift)
827
{
828
    int i, j;
829
    int32_t pred;
830

  
831
    for(i=0; i<order; i++) {
832
        res[i] = smp[i];
833
    }
834
    for(i=order; i<n; i++) {
835
        pred = 0;
836
        for(j=0; j<order; j++) {
837
            pred += coefs[j] * smp[i-j-1];
838
        }
839
        res[i] = smp[i] - (pred >> shift);
840
    }
841
}
842

  
405 843
static int get_max_p_order(int max_porder, int n, int order)
406 844
{
407 845
    int porder, max_parts;
......
419 857

  
420 858
static int encode_residual(FlacEncodeContext *ctx, int ch)
421 859
{
422
    int i, opt_order, porder, max_porder, n;
860
    int i, n;
861
    int min_order, max_order, opt_order, precision;
862
    int porder, min_porder, max_porder;
423 863
    FlacFrame *frame;
424 864
    FlacSubframe *sub;
425
    uint32_t bits[5];
865
    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
866
    int shift[MAX_LPC_ORDER];
426 867
    int32_t *res, *smp;
427 868

  
428 869
    frame = &ctx->frame;
......
448 889
        return sub->obits * n;
449 890
    }
450 891

  
451
    max_porder = 3;
892
    min_order = ctx->options.min_prediction_order;
893
    max_order = ctx->options.max_prediction_order;
894
    min_porder = ctx->options.min_partition_order;
895
    max_porder = ctx->options.max_partition_order;
896
    precision = ctx->options.lpc_coeff_precision;
452 897

  
453 898
    /* FIXED */
454
    opt_order = 0;
455
    bits[0] = UINT32_MAX;
456
    for(i=0; i<=4; i++) {
457
        encode_residual_fixed(res, smp, n, i);
458
        porder = get_max_p_order(max_porder, n, i);
459
        bits[i] = calc_rice_params_fixed(&sub->rc, porder, res, n, i, sub->obits);
460
        if(bits[i] < bits[opt_order]) {
461
            opt_order = i;
899
    if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {
900
        uint32_t bits[MAX_FIXED_ORDER+1];
901
        if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;
902
        opt_order = 0;
903
        bits[0] = UINT32_MAX;
904
        for(i=min_order; i<=max_order; i++) {
905
            encode_residual_fixed(res, smp, n, i);
906
            porder = get_max_p_order(max_porder, n, i);
907
            bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, porder, res,
908
                                             n, i, sub->obits);
909
            if(bits[i] < bits[opt_order]) {
910
                opt_order = i;
911
            }
462 912
        }
913
        sub->order = opt_order;
914
        sub->type = FLAC_SUBFRAME_FIXED;
915
        sub->type_code = sub->type | sub->order;
916
        if(sub->order != max_order) {
917
            encode_residual_fixed(res, smp, n, sub->order);
918
            porder = get_max_p_order(max_porder, n, sub->order);
919
            return calc_rice_params_fixed(&sub->rc, min_porder, porder, res, n,
920
                                          sub->order, sub->obits);
921
        }
922
        return bits[sub->order];
463 923
    }
464
    sub->order = opt_order;
465
    sub->type = FLAC_SUBFRAME_FIXED;
466
    sub->type_code = sub->type | sub->order;
467
    if(sub->order != 4) {
468
        encode_residual_fixed(res, smp, n, sub->order);
469
        porder = get_max_p_order(max_porder, n, sub->order);
470
        calc_rice_params_fixed(&sub->rc, porder, res, n, sub->order, sub->obits);
924

  
925
    /* LPC */
926
    sub->order = lpc_calc_coefs(smp, n, max_order, precision, coefs, shift);
927
    sub->type = FLAC_SUBFRAME_LPC;
928
    sub->type_code = sub->type | (sub->order-1);
929
    sub->shift = shift[sub->order-1];
930
    for(i=0; i<sub->order; i++) {
931
        sub->coefs[i] = coefs[sub->order-1][i];
471 932
    }
472
    return bits[sub->order];
933
    porder = get_max_p_order(max_porder, n, sub->order);
934
    encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
935
    return calc_rice_params_lpc(&sub->rc, 0, porder, res, n, sub->order,
936
                                sub->obits, precision);
473 937
}
474 938

  
475 939
static int encode_residual_v(FlacEncodeContext *ctx, int ch)
......
509 973
    uint64_t score[4];
510 974
    int k;
511 975

  
512
    /* calculate sum of squares for each channel */
976
    /* calculate sum of 2nd order residual for each channel */
513 977
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
514 978
    for(i=2; i<n; i++) {
515 979
        lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
......
519 983
        sum[0] += ABS(lt);
520 984
        sum[1] += ABS(rt);
521 985
    }
986
    /* estimate bit counts */
522 987
    for(i=0; i<4; i++) {
523 988
        k = find_optimal_param(2*sum[i], n);
524 989
        sum[i] = rice_encode_count(2*sum[i], n, k);
......
731 1196
    output_residual(ctx, ch);
732 1197
}
733 1198

  
1199
static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
1200
{
1201
    int i, cbits;
1202
    FlacFrame *frame;
1203
    FlacSubframe *sub;
1204

  
1205
    frame = &ctx->frame;
1206
    sub = &frame->subframes[ch];
1207

  
1208
    /* warm-up samples */
1209
    for(i=0; i<sub->order; i++) {
1210
        put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1211
    }
1212

  
1213
    /* LPC coefficients */
1214
    cbits = ctx->options.lpc_coeff_precision;
1215
    put_bits(&ctx->pb, 4, cbits-1);
1216
    put_sbits(&ctx->pb, 5, sub->shift);
1217
    for(i=0; i<sub->order; i++) {
1218
        put_sbits(&ctx->pb, cbits, sub->coefs[i]);
1219
    }
1220

  
1221
    /* residual */
1222
    output_residual(ctx, ch);
1223
}
1224

  
734 1225
static void output_subframes(FlacEncodeContext *s)
735 1226
{
736 1227
    FlacFrame *frame;
......
754 1245
            output_subframe_verbatim(s, ch);
755 1246
        } else if(sub->type == FLAC_SUBFRAME_FIXED) {
756 1247
            output_subframe_fixed(s, ch);
1248
        } else if(sub->type == FLAC_SUBFRAME_LPC) {
1249
            output_subframe_lpc(s, ch);
757 1250
        }
758 1251
    }
759 1252
}

Also available in: Unified diff