libavcodec/psymodel.c
78e65cd7
 /*
  * audio encoder psychoacoustic model
  * Copyright (C) 2008 Konstantin Shishkov
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
1d9c2dc8
 #include <string.h>
 
78e65cd7
 #include "avcodec.h"
 #include "psymodel.h"
 #include "iirfilter.h"
1d9c2dc8
 #include "libavutil/mem.h"
78e65cd7
 
 extern const FFPsyModel ff_aac_psy_model;
 
0bc01cc9
 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
                         const uint8_t **bands, const int* num_bands,
                         int num_groups, const uint8_t *group_map)
78e65cd7
 {
0bc01cc9
     int i, j, k = 0;
 
78e65cd7
     ctx->avctx = avctx;
d42a6192
     ctx->ch        = av_mallocz_array(sizeof(ctx->ch[0]), avctx->channels * 2);
     ctx->group     = av_mallocz_array(sizeof(ctx->group[0]), num_groups);
     ctx->bands     = av_malloc_array (sizeof(ctx->bands[0]),      num_lens);
     ctx->num_bands = av_malloc_array (sizeof(ctx->num_bands[0]),  num_lens);
ca203e99
     ctx->cutoff    = avctx->cutoff;
03927cb7
 
     if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
         ff_psy_end(ctx);
         return AVERROR(ENOMEM);
     }
 
78e65cd7
     memcpy(ctx->bands,     bands,     sizeof(ctx->bands[0])     *  num_lens);
     memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) *  num_lens);
0bc01cc9
 
     /* assign channels to groups (with virtual channels for coupling) */
     for (i = 0; i < num_groups; i++) {
         /* NOTE: Add 1 to handle the AAC chan_config without modification.
          *       This has the side effect of allowing an array of 0s to map
          *       to one channel per group.
          */
         ctx->group[i].num_ch = group_map[i] + 1;
         for (j = 0; j < ctx->group[i].num_ch * 2; j++)
             ctx->group[i].ch[j]  = &ctx->ch[k++];
     }
 
fd257dc4
     switch (ctx->avctx->codec_id) {
36ef5369
     case AV_CODEC_ID_AAC:
78e65cd7
         ctx->model = &ff_aac_psy_model;
         break;
     }
fd257dc4
     if (ctx->model->init)
78e65cd7
         return ctx->model->init(ctx);
     return 0;
 }
 
0bc01cc9
 FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel)
 {
     int i = 0, ch = 0;
 
     while (ch <= channel)
         ch += ctx->group[i++].num_ch;
 
     return &ctx->group[i-1];
 }
 
78e65cd7
 av_cold void ff_psy_end(FFPsyContext *ctx)
 {
f4aa8085
     if (ctx->model && ctx->model->end)
78e65cd7
         ctx->model->end(ctx);
     av_freep(&ctx->bands);
     av_freep(&ctx->num_bands);
0bc01cc9
     av_freep(&ctx->group);
     av_freep(&ctx->ch);
78e65cd7
 }
 
 typedef struct FFPsyPreprocessContext{
     AVCodecContext *avctx;
     float stereo_att;
     struct FFIIRFilterCoeffs *fcoeffs;
     struct FFIIRFilterState **fstate;
7d29c6ee
     struct FFIIRFilterContext fiir;
78e65cd7
 }FFPsyPreprocessContext;
 
 #define FILT_ORDER 4
 
 av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
 {
     FFPsyPreprocessContext *ctx;
     int i;
636da41a
     float cutoff_coeff = 0;
99d61d34
     ctx        = av_mallocz(sizeof(FFPsyPreprocessContext));
03927cb7
     if (!ctx)
         return NULL;
78e65cd7
     ctx->avctx = avctx;
 
01ecb717
     /* AAC has its own LP method */
     if (avctx->codec_id != AV_CODEC_ID_AAC) {
323d3752
         if (avctx->cutoff > 0)
             cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
 
         if (cutoff_coeff && cutoff_coeff < 0.98)
         ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
                                                  FF_FILTER_MODE_LOWPASS, FILT_ORDER,
                                                  cutoff_coeff, 0.0, 0.0);
         if (ctx->fcoeffs) {
8dbffda0
             ctx->fstate = av_mallocz_array(sizeof(ctx->fstate[0]), avctx->channels);
             if (!ctx->fstate) {
                 av_free(ctx->fcoeffs);
                 av_free(ctx);
                 return NULL;
             }
323d3752
             for (i = 0; i < avctx->channels; i++)
                 ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
         }
01ecb717
     }
7d29c6ee
 
     ff_iir_filter_init(&ctx->fiir);
 
78e65cd7
     return ctx;
 }
 
9b8e2a87
 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
78e65cd7
 {
9b8e2a87
     int ch;
     int frame_size = ctx->avctx->frame_size;
7d29c6ee
     FFIIRFilterContext *iir = &ctx->fiir;
9b8e2a87
 
fd257dc4
     if (ctx->fstate) {
c8f47d8b
         for (ch = 0; ch < channels; ch++)
7d29c6ee
             iir->filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
                             &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
78e65cd7
     }
 }
 
 av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
 {
     int i;
43fb16cf
     ff_iir_filter_free_coeffsp(&ctx->fcoeffs);
78e65cd7
     if (ctx->fstate)
         for (i = 0; i < ctx->avctx->channels; i++)
f43ac027
             ff_iir_filter_free_statep(&ctx->fstate[i]);
78e65cd7
     av_freep(&ctx->fstate);
771c86c1
     av_free(ctx);
78e65cd7
 }