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.

131 lines
4.4KB

  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. #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. FFPsyWindowInfo ff_psy_suggest_window(FFPsyContext *ctx,
  45. const int16_t *audio, const int16_t *la,
  46. int channel, int prev_type)
  47. {
  48. return ctx->model->window(ctx, audio, la, channel, prev_type);
  49. }
  50. void ff_psy_set_band_info(FFPsyContext *ctx, int channel,
  51. const float *coeffs, FFPsyWindowInfo *wi)
  52. {
  53. ctx->model->analyze(ctx, channel, coeffs, wi);
  54. }
  55. av_cold void ff_psy_end(FFPsyContext *ctx)
  56. {
  57. if (ctx->model->end)
  58. ctx->model->end(ctx);
  59. av_freep(&ctx->bands);
  60. av_freep(&ctx->num_bands);
  61. av_freep(&ctx->psy_bands);
  62. }
  63. typedef struct FFPsyPreprocessContext{
  64. AVCodecContext *avctx;
  65. float stereo_att;
  66. struct FFIIRFilterCoeffs *fcoeffs;
  67. struct FFIIRFilterState **fstate;
  68. }FFPsyPreprocessContext;
  69. #define FILT_ORDER 4
  70. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
  71. {
  72. FFPsyPreprocessContext *ctx;
  73. int i;
  74. float cutoff_coeff;
  75. ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
  76. ctx->avctx = avctx;
  77. if (avctx->cutoff > 0)
  78. cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
  79. else if (avctx->flags & CODEC_FLAG_QSCALE)
  80. cutoff_coeff = 1.0f / av_clip(1 + avctx->global_quality / FF_QUALITY_SCALE, 1, 8);
  81. else
  82. cutoff_coeff = avctx->bit_rate / (4.0f * avctx->sample_rate * avctx->channels);
  83. ctx->fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH, FF_FILTER_MODE_LOWPASS,
  84. FILT_ORDER, cutoff_coeff, 0.0, 0.0);
  85. if (ctx->fcoeffs) {
  86. ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
  87. for (i = 0; i < avctx->channels; i++)
  88. ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
  89. }
  90. return ctx;
  91. }
  92. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
  93. const int16_t *audio, int16_t *dest,
  94. int tag, int channels)
  95. {
  96. int ch, i;
  97. if (ctx->fstate) {
  98. for (ch = 0; ch < channels; ch++)
  99. ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
  100. audio + ch, ctx->avctx->channels,
  101. dest + ch, ctx->avctx->channels);
  102. } else {
  103. for (ch = 0; ch < channels; ch++)
  104. for (i = 0; i < ctx->avctx->frame_size; i++)
  105. dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
  106. }
  107. }
  108. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
  109. {
  110. int i;
  111. ff_iir_filter_free_coeffs(ctx->fcoeffs);
  112. if (ctx->fstate)
  113. for (i = 0; i < ctx->avctx->channels; i++)
  114. ff_iir_filter_free_state(ctx->fstate[i]);
  115. av_freep(&ctx->fstate);
  116. }