Commit | Line | Data |
---|---|---|
78e65cd7 AC |
1 | /* |
2 | * audio encoder psychoacoustic model | |
3 | * Copyright (C) 2008 Konstantin Shishkov | |
4 | * | |
5 | * This file is part of FFmpeg. | |
6 | * | |
7 | * FFmpeg 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. | |
11 | * | |
12 | * FFmpeg 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. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with FFmpeg; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include "avcodec.h" | |
23 | #include "psymodel.h" | |
24 | #include "iirfilter.h" | |
25 | ||
26 | extern const FFPsyModel ff_aac_psy_model; | |
27 | ||
28 | av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, | |
29 | int num_lens, | |
30 | const uint8_t **bands, const int* num_bands) | |
31 | { | |
32 | ctx->avctx = avctx; | |
33 | ctx->psy_bands = av_mallocz(sizeof(FFPsyBand) * PSY_MAX_BANDS * avctx->channels); | |
34 | ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens); | |
35 | ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens); | |
36 | memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens); | |
37 | memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens); | |
fd257dc4 | 38 | switch (ctx->avctx->codec_id) { |
78e65cd7 AC |
39 | case CODEC_ID_AAC: |
40 | ctx->model = &ff_aac_psy_model; | |
41 | break; | |
42 | } | |
fd257dc4 | 43 | if (ctx->model->init) |
78e65cd7 AC |
44 | return ctx->model->init(ctx); |
45 | return 0; | |
46 | } | |
47 | ||
48 | FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx, | |
49 | const int16_t *audio, const int16_t *la, | |
50 | int channel, int prev_type) | |
51 | { | |
52 | return ctx->model->window(ctx, audio, la, channel, prev_type); | |
53 | } | |
54 | ||
55 | void ff_psy_set_band_info(FFPsyContext *ctx, int channel, | |
56 | const float *coeffs, FFPsyWindowInfo *wi) | |
57 | { | |
58 | ctx->model->analyze(ctx, channel, coeffs, wi); | |
59 | } | |
60 | ||
61 | av_cold void ff_psy_end(FFPsyContext *ctx) | |
62 | { | |
fd257dc4 | 63 | if (ctx->model->end) |
78e65cd7 AC |
64 | ctx->model->end(ctx); |
65 | av_freep(&ctx->bands); | |
66 | av_freep(&ctx->num_bands); | |
67 | av_freep(&ctx->psy_bands); | |
68 | } | |
69 | ||
70 | typedef struct FFPsyPreprocessContext{ | |
71 | AVCodecContext *avctx; | |
72 | float stereo_att; | |
73 | struct FFIIRFilterCoeffs *fcoeffs; | |
74 | struct FFIIRFilterState **fstate; | |
75 | }FFPsyPreprocessContext; | |
76 | ||
77 | #define FILT_ORDER 4 | |
78 | ||
79 | av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx) | |
80 | { | |
81 | FFPsyPreprocessContext *ctx; | |
82 | int i; | |
83 | float cutoff_coeff; | |
99d61d34 | 84 | ctx = av_mallocz(sizeof(FFPsyPreprocessContext)); |
78e65cd7 AC |
85 | ctx->avctx = avctx; |
86 | ||
2876a9db AC |
87 | if (avctx->cutoff > 0) |
88 | cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate; | |
89 | else if (avctx->flags & CODEC_FLAG_QSCALE) | |
78e65cd7 AC |
90 | cutoff_coeff = 1.0f / av_clip(1 + avctx->global_quality / FF_QUALITY_SCALE, 1, 8); |
91 | else | |
92 | cutoff_coeff = avctx->bit_rate / (4.0f * avctx->sample_rate * avctx->channels); | |
93 | ||
94 | ctx->fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH, FF_FILTER_MODE_LOWPASS, | |
99d61d34 | 95 | FILT_ORDER, cutoff_coeff, 0.0, 0.0); |
fd257dc4 | 96 | if (ctx->fcoeffs) { |
78e65cd7 | 97 | ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels); |
fd257dc4 | 98 | for (i = 0; i < avctx->channels; i++) |
78e65cd7 AC |
99 | ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER); |
100 | } | |
101 | return ctx; | |
102 | } | |
103 | ||
104 | void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, | |
105 | const int16_t *audio, int16_t *dest, | |
106 | int tag, int channels) | |
107 | { | |
108 | int ch, i; | |
fd257dc4 | 109 | if (ctx->fstate) { |
c8f47d8b | 110 | for (ch = 0; ch < channels; ch++) |
78e65cd7 AC |
111 | ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size, |
112 | audio + ch, ctx->avctx->channels, | |
113 | dest + ch, ctx->avctx->channels); | |
fd257dc4 | 114 | } else { |
c8f47d8b | 115 | for (ch = 0; ch < channels; ch++) |
fd257dc4 | 116 | for (i = 0; i < ctx->avctx->frame_size; i++) |
78e65cd7 | 117 | dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch]; |
78e65cd7 AC |
118 | } |
119 | } | |
120 | ||
121 | av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx) | |
122 | { | |
123 | int i; | |
124 | ff_iir_filter_free_coeffs(ctx->fcoeffs); | |
125 | if (ctx->fstate) | |
126 | for (i = 0; i < ctx->avctx->channels; i++) | |
127 | ff_iir_filter_free_state(ctx->fstate[i]); | |
128 | av_freep(&ctx->fstate); | |
129 | } | |
130 |