Revision 7101b185

View differences:

libavcodec/alacenc.c
378 378
static av_cold int alac_encode_init(AVCodecContext *avctx)
379 379
{
380 380
    AlacEncodeContext *s    = avctx->priv_data;
381
    int ret;
381 382
    uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
382 383

  
383 384
    avctx->frame_size      = DEFAULT_FRAME_SIZE;
......
455 456
    avctx->coded_frame->key_frame = 1;
456 457

  
457 458
    s->avctx = avctx;
458
    ff_lpc_init(&s->lpc_ctx);
459
    ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size, s->max_prediction_order,
460
                      AV_LPC_TYPE_LEVINSON);
459 461

  
460
    return 0;
462
    return ret;
461 463
}
462 464

  
463 465
static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
......
513 515

  
514 516
static av_cold int alac_encode_close(AVCodecContext *avctx)
515 517
{
518
    AlacEncodeContext *s = avctx->priv_data;
519
    ff_lpc_end(&s->lpc_ctx);
516 520
    av_freep(&avctx->extradata);
517 521
    avctx->extradata_size = 0;
518 522
    av_freep(&avctx->coded_frame);
libavcodec/flacenc.c
211 211
    int freq = avctx->sample_rate;
212 212
    int channels = avctx->channels;
213 213
    FlacEncodeContext *s = avctx->priv_data;
214
    int i, level;
214
    int i, level, ret;
215 215
    uint8_t *streaminfo;
216 216

  
217 217
    s->avctx = avctx;
218 218

  
219
    ff_lpc_init(&s->lpc_ctx);
220

  
221 219
    if (avctx->sample_fmt != AV_SAMPLE_FMT_S16)
222 220
        return -1;
223 221

  
......
438 436
    if (!avctx->coded_frame)
439 437
        return AVERROR(ENOMEM);
440 438

  
439
    ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
440
                      s->options.max_prediction_order, AV_LPC_TYPE_LEVINSON);
441

  
441 442
    dprint_compression_options(s);
442 443

  
443
    return 0;
444
    return ret;
444 445
}
445 446

  
446 447

  
......
1316 1317
    if (avctx->priv_data) {
1317 1318
        FlacEncodeContext *s = avctx->priv_data;
1318 1319
        av_freep(&s->md5ctx);
1320
        ff_lpc_end(&s->lpc_ctx);
1319 1321
    }
1320 1322
    av_freep(&avctx->extradata);
1321 1323
    avctx->extradata_size = 0;
libavcodec/lpc.c
28 28
/**
29 29
 * Apply Welch window function to audio block
30 30
 */
31
static void apply_welch_window(const int32_t *data, int len, double *w_data)
31
static void apply_welch_window_c(const int32_t *data, int len, double *w_data)
32 32
{
33 33
    int i, n2;
34 34
    double w;
......
54 54
 * Calculate autocorrelation data from audio samples
55 55
 * A Welch window function is applied before calculation.
56 56
 */
57
static void lpc_compute_autocorr_c(const int32_t *data, int len, int lag,
57
static void lpc_compute_autocorr_c(const double *data, int len, int lag,
58 58
                             double *autoc)
59 59
{
60 60
    int i, j;
61
    double tmp[len + lag + 1];
62
    double *data1= tmp + lag;
63

  
64
    apply_welch_window(data, len, data1);
65

  
66
    for(j=0; j<lag; j++)
67
        data1[j-lag]= 0.0;
68
    data1[len] = 0.0;
69 61

  
70 62
    for(j=0; j<lag; j+=2){
71 63
        double sum0 = 1.0, sum1 = 1.0;
72 64
        for(i=j; i<len; i++){
73
            sum0 += data1[i] * data1[i-j];
74
            sum1 += data1[i] * data1[i-j-1];
65
            sum0 += data[i] * data[i-j];
66
            sum1 += data[i] * data[i-j-1];
75 67
        }
76 68
        autoc[j  ] = sum0;
77 69
        autoc[j+1] = sum1;
......
80 72
    if(j==lag){
81 73
        double sum = 1.0;
82 74
        for(i=j-1; i<len; i+=2){
83
            sum += data1[i  ] * data1[i-j  ]
84
                 + data1[i+1] * data1[i-j+1];
75
            sum += data[i  ] * data[i-j  ]
76
                 + data[i+1] * data[i-j+1];
85 77
        }
86 78
        autoc[j] = sum;
87 79
    }
......
177 169
    assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER &&
178 170
           lpc_type > AV_LPC_TYPE_FIXED);
179 171

  
172
    /* reinit LPC context if parameters have changed */
173
    if (blocksize != s->blocksize || max_order != s->max_order ||
174
        lpc_type  != s->lpc_type) {
175
        ff_lpc_end(s);
176
        ff_lpc_init(s, blocksize, max_order, lpc_type);
177
    }
178

  
180 179
    if (lpc_type == AV_LPC_TYPE_LEVINSON) {
181
        s->lpc_compute_autocorr(samples, blocksize, max_order, autoc);
180
        double *windowed_samples = s->windowed_samples + max_order;
181

  
182
        s->lpc_apply_welch_window(samples, blocksize, windowed_samples);
183

  
184
        s->lpc_compute_autocorr(windowed_samples, blocksize, max_order, autoc);
182 185

  
183 186
        compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
184 187

  
......
236 239
    return opt_order;
237 240
}
238 241

  
239
av_cold void ff_lpc_init(LPCContext *s)
242
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
243
                        enum AVLPCType lpc_type)
240 244
{
245
    s->blocksize = blocksize;
246
    s->max_order = max_order;
247
    s->lpc_type  = lpc_type;
248

  
249
    if (lpc_type == AV_LPC_TYPE_LEVINSON) {
250
        s->windowed_samples = av_mallocz((blocksize + max_order + 2) *
251
                                         sizeof(*s->windowed_samples));
252
        if (!s->windowed_samples)
253
            return AVERROR(ENOMEM);
254
    } else {
255
        s->windowed_samples = NULL;
256
    }
257

  
258
    s->lpc_apply_welch_window = apply_welch_window_c;
241 259
    s->lpc_compute_autocorr = lpc_compute_autocorr_c;
242 260

  
243 261
    if (HAVE_MMX)
244 262
        ff_lpc_init_x86(s);
263

  
264
    return 0;
265
}
266

  
267
av_cold void ff_lpc_end(LPCContext *s)
268
{
269
    av_freep(&s->windowed_samples);
245 270
}
libavcodec/lpc.h
37 37

  
38 38

  
39 39
typedef struct LPCContext {
40
    int blocksize;
41
    int max_order;
42
    enum AVLPCType lpc_type;
43
    double *windowed_samples;
44

  
45
    /**
46
     * Apply a Welch window to an array of input samples.
47
     * The output samples have the same scale as the input, but are in double
48
     * sample format.
49
     * @param data    input samples
50
     * @param len     number of input samples
51
     * @param w_data  output samples
52
     */
53
    void (*lpc_apply_welch_window)(const int32_t *data, int len,
54
                                   double *w_data);
40 55
    /**
41 56
     * Perform autocorrelation on input samples with delay of 0 to lag.
42 57
     * @param data  input samples.
43
     *              no alignment needed.
58
     *              constraints: no alignment needed, but must have have at
59
     *              least lag*sizeof(double) valid bytes preceeding it, and
60
     *              size must be at least (len+1)*sizeof(double) if data is
61
     *              16-byte aligned or (len+2)*sizeof(double) if data is
62
     *              unaligned.
44 63
     * @param len   number of input samples to process
45 64
     * @param lag   maximum delay to calculate
46 65
     * @param autoc output autocorrelation coefficients.
47 66
     *              constraints: array size must be at least lag+1.
48 67
     */
49
    void (*lpc_compute_autocorr)(const int32_t *data, int len, int lag,
68
    void (*lpc_compute_autocorr)(const double *data, int len, int lag,
50 69
                                 double *autoc);
51 70
} LPCContext;
52 71

  
......
64 83
/**
65 84
 * Initialize LPCContext.
66 85
 */
67
void ff_lpc_init(LPCContext *s);
86
int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
87
                enum AVLPCType lpc_type);
68 88
void ff_lpc_init_x86(LPCContext *s);
69 89

  
90
/**
91
 * Uninitialize LPCContext.
92
 */
93
void ff_lpc_end(LPCContext *s);
94

  
70 95
#ifdef LPC_USE_DOUBLE
71 96
#define LPC_TYPE double
72 97
#else
libavcodec/ra144enc.c
36 36
static av_cold int ra144_encode_init(AVCodecContext * avctx)
37 37
{
38 38
    RA144Context *ractx;
39
    int ret;
39 40

  
40 41
    if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
41 42
        av_log(avctx, AV_LOG_ERROR, "invalid sample format\n");
......
52 53
    ractx->lpc_coef[0] = ractx->lpc_tables[0];
53 54
    ractx->lpc_coef[1] = ractx->lpc_tables[1];
54 55
    ractx->avctx = avctx;
55
    ff_lpc_init(&ractx->lpc_ctx);
56
    ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
57
                      AV_LPC_TYPE_LEVINSON);
58
    return ret;
59
}
60

  
61

  
62
static av_cold int ra144_encode_close(AVCodecContext *avctx)
63
{
64
    RA144Context *ractx = avctx->priv_data;
65
    ff_lpc_end(&ractx->lpc_ctx);
56 66
    return 0;
57 67
}
58 68

  
......
506 516
    sizeof(RA144Context),
507 517
    ra144_encode_init,
508 518
    ra144_encode_frame,
519
    ra144_encode_close,
509 520
    .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"),
510 521
};
libavcodec/x86/lpc_mmx.c
69 69
#undef WELCH
70 70
}
71 71

  
72
static void lpc_compute_autocorr_sse2(const int32_t *data, int len, int lag,
72
static void lpc_compute_autocorr_sse2(const double *data, int len, int lag,
73 73
                                   double *autoc)
74 74
{
75
    double tmp[len + lag + 2];
76
    double *data1 = tmp + lag;
77 75
    int j;
78 76

  
79
    if((x86_reg)data1 & 15)
80
        data1++;
81

  
82
    apply_welch_window_sse2(data, len, data1);
83

  
84
    for(j=0; j<lag; j++)
85
        data1[j-lag]= 0.0;
86
    data1[len] = 0.0;
77
    if((x86_reg)data & 15)
78
        data++;
87 79

  
88 80
    for(j=0; j<lag; j+=2){
89 81
        x86_reg i = -len*sizeof(double);
......
114 106
                "movsd     %%xmm1,  8(%1)           \n\t"
115 107
                "movsd     %%xmm2, 16(%1)           \n\t"
116 108
                :"+&r"(i)
117
                :"r"(autoc+j), "r"(data1+len), "r"(data1+len-j)
109
                :"r"(autoc+j), "r"(data+len), "r"(data+len-j)
118 110
                :"memory"
119 111
            );
120 112
        } else {
......
137 129
                "movsd     %%xmm0, %1               \n\t"
138 130
                "movsd     %%xmm1, %2               \n\t"
139 131
                :"+&r"(i), "=m"(autoc[j]), "=m"(autoc[j+1])
140
                :"r"(data1+len), "r"(data1+len-j)
132
                :"r"(data+len), "r"(data+len-j)
141 133
            );
142 134
        }
143 135
    }
......
148 140
    int mm_flags = av_get_cpu_flags();
149 141

  
150 142
    if (mm_flags & (AV_CPU_FLAG_SSE2|AV_CPU_FLAG_SSE2SLOW)) {
143
        c->lpc_apply_welch_window = apply_welch_window_sse2;
151 144
        c->lpc_compute_autocorr = lpc_compute_autocorr_sse2;
152 145
    }
153 146
}

Also available in: Unified diff