]> git.sesse.net Git - ffmpeg/blob - libavcodec/psymodel.c
H.264: disable 2tap qpel with CODEC_FLAG2_FAST and >8-bit
[ffmpeg] / libavcodec / psymodel.c
1 /*
2  * audio encoder psychoacoustic model
3  * Copyright (C) 2008 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
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);
38     switch (ctx->avctx->codec_id) {
39     case CODEC_ID_AAC:
40         ctx->model = &ff_aac_psy_model;
41         break;
42     }
43     if (ctx->model->init)
44         return ctx->model->init(ctx);
45     return 0;
46 }
47
48 av_cold void ff_psy_end(FFPsyContext *ctx)
49 {
50     if (ctx->model->end)
51         ctx->model->end(ctx);
52     av_freep(&ctx->bands);
53     av_freep(&ctx->num_bands);
54     av_freep(&ctx->psy_bands);
55 }
56
57 typedef struct FFPsyPreprocessContext{
58     AVCodecContext *avctx;
59     float stereo_att;
60     struct FFIIRFilterCoeffs *fcoeffs;
61     struct FFIIRFilterState **fstate;
62 }FFPsyPreprocessContext;
63
64 #define FILT_ORDER 4
65
66 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
67 {
68     FFPsyPreprocessContext *ctx;
69     int i;
70     float cutoff_coeff = 0;
71     ctx        = av_mallocz(sizeof(FFPsyPreprocessContext));
72     ctx->avctx = avctx;
73
74     if (avctx->cutoff > 0)
75         cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
76
77     if (cutoff_coeff)
78     ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
79                                              FF_FILTER_MODE_LOWPASS, FILT_ORDER,
80                                              cutoff_coeff, 0.0, 0.0);
81     if (ctx->fcoeffs) {
82         ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
83         for (i = 0; i < avctx->channels; i++)
84             ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
85     }
86     return ctx;
87 }
88
89 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
90                        const int16_t *audio, int16_t *dest,
91                        int tag, int channels)
92 {
93     int ch, i;
94     if (ctx->fstate) {
95         for (ch = 0; ch < channels; ch++)
96             ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
97                           audio + ch, ctx->avctx->channels,
98                           dest  + ch, ctx->avctx->channels);
99     } else {
100         for (ch = 0; ch < channels; ch++)
101             for (i = 0; i < ctx->avctx->frame_size; i++)
102                 dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
103     }
104 }
105
106 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
107 {
108     int i;
109     ff_iir_filter_free_coeffs(ctx->fcoeffs);
110     if (ctx->fstate)
111         for (i = 0; i < ctx->avctx->channels; i++)
112             ff_iir_filter_free_state(ctx->fstate[i]);
113     av_freep(&ctx->fstate);
114     av_free(ctx);
115 }
116