You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.8KB

  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. #include "avcodec.h"
  22. #include "psymodel.h"
  23. #include "iirfilter.h"
  24. extern const FFPsyModel ff_aac_psy_model;
  25. av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx,
  26. int num_lens,
  27. const uint8_t **bands, const int* num_bands)
  28. {
  29. ctx->avctx = avctx;
  30. ctx->psy_bands = av_mallocz(sizeof(FFPsyBand) * PSY_MAX_BANDS * avctx->channels);
  31. ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens);
  32. ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
  33. memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
  34. memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
  35. switch (ctx->avctx->codec_id) {
  36. case CODEC_ID_AAC:
  37. ctx->model = &ff_aac_psy_model;
  38. break;
  39. }
  40. if (ctx->model->init)
  41. return ctx->model->init(ctx);
  42. return 0;
  43. }
  44. av_cold void ff_psy_end(FFPsyContext *ctx)
  45. {
  46. if (ctx->model->end)
  47. ctx->model->end(ctx);
  48. av_freep(&ctx->bands);
  49. av_freep(&ctx->num_bands);
  50. av_freep(&ctx->psy_bands);
  51. }
  52. typedef struct FFPsyPreprocessContext{
  53. AVCodecContext *avctx;
  54. float stereo_att;
  55. struct FFIIRFilterCoeffs *fcoeffs;
  56. struct FFIIRFilterState **fstate;
  57. }FFPsyPreprocessContext;
  58. #define FILT_ORDER 4
  59. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
  60. {
  61. FFPsyPreprocessContext *ctx;
  62. int i;
  63. float cutoff_coeff = 0;
  64. ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
  65. ctx->avctx = avctx;
  66. if (avctx->cutoff > 0)
  67. cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
  68. if (cutoff_coeff)
  69. ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
  70. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  71. cutoff_coeff, 0.0, 0.0);
  72. if (ctx->fcoeffs) {
  73. ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
  74. for (i = 0; i < avctx->channels; i++)
  75. ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
  76. }
  77. return ctx;
  78. }
  79. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
  80. const int16_t *audio, int16_t *dest,
  81. int tag, int channels)
  82. {
  83. int ch, i;
  84. if (ctx->fstate) {
  85. for (ch = 0; ch < channels; ch++)
  86. ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
  87. audio + ch, ctx->avctx->channels,
  88. dest + ch, ctx->avctx->channels);
  89. } else {
  90. for (ch = 0; ch < channels; ch++)
  91. for (i = 0; i < ctx->avctx->frame_size; i++)
  92. dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
  93. }
  94. }
  95. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
  96. {
  97. int i;
  98. ff_iir_filter_free_coeffs(ctx->fcoeffs);
  99. if (ctx->fstate)
  100. for (i = 0; i < ctx->avctx->channels; i++)
  101. ff_iir_filter_free_state(ctx->fstate[i]);
  102. av_freep(&ctx->fstate);
  103. av_free(ctx);
  104. }