2 * audio encoder psychoacoustic model
3 * Copyright (C) 2008 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "iirfilter.h"
26 extern const FFPsyModel ff_aac_psy_model;
28 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
29 const uint8_t **bands, const int* num_bands,
30 int num_groups, const uint8_t *group_map)
35 ctx->ch = av_mallocz(sizeof(ctx->ch[0]) * avctx->channels * 2);
36 ctx->group = av_mallocz(sizeof(ctx->group[0]) * num_groups);
37 ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens);
38 ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
39 memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
40 memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
42 /* assign channels to groups (with virtual channels for coupling) */
43 for (i = 0; i < num_groups; i++) {
44 /* NOTE: Add 1 to handle the AAC chan_config without modification.
45 * This has the side effect of allowing an array of 0s to map
46 * to one channel per group.
48 ctx->group[i].num_ch = group_map[i] + 1;
49 for (j = 0; j < ctx->group[i].num_ch * 2; j++)
50 ctx->group[i].ch[j] = &ctx->ch[k++];
53 switch (ctx->avctx->codec_id) {
55 ctx->model = &ff_aac_psy_model;
59 return ctx->model->init(ctx);
63 FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel)
68 ch += ctx->group[i++].num_ch;
70 return &ctx->group[i-1];
73 av_cold void ff_psy_end(FFPsyContext *ctx)
77 av_freep(&ctx->bands);
78 av_freep(&ctx->num_bands);
79 av_freep(&ctx->group);
83 typedef struct FFPsyPreprocessContext{
84 AVCodecContext *avctx;
86 struct FFIIRFilterCoeffs *fcoeffs;
87 struct FFIIRFilterState **fstate;
88 }FFPsyPreprocessContext;
92 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
94 FFPsyPreprocessContext *ctx;
96 float cutoff_coeff = 0;
97 ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
100 if (avctx->cutoff > 0)
101 cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
104 ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
105 FF_FILTER_MODE_LOWPASS, FILT_ORDER,
106 cutoff_coeff, 0.0, 0.0);
108 ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
109 for (i = 0; i < avctx->channels; i++)
110 ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
115 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
116 const int16_t *audio, int16_t *dest,
117 int tag, int channels)
121 for (ch = 0; ch < channels; ch++)
122 ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
123 audio + ch, ctx->avctx->channels,
124 dest + ch, ctx->avctx->channels);
126 for (ch = 0; ch < channels; ch++)
127 for (i = 0; i < ctx->avctx->frame_size; i++)
128 dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
132 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
135 ff_iir_filter_free_coeffs(ctx->fcoeffs);
137 for (i = 0; i < ctx->avctx->channels; i++)
138 ff_iir_filter_free_state(ctx->fstate[i]);
139 av_freep(&ctx->fstate);