psymodel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <string.h>
  22. #include "avcodec.h"
  23. #include "psymodel.h"
  24. #include "iirfilter.h"
  25. #include "libavutil/mem.h"
  26. extern const FFPsyModel ff_aac_psy_model;
  27. av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
  28. const uint8_t **bands, const int* num_bands,
  29. int num_groups, const uint8_t *group_map)
  30. {
  31. int i, j, k = 0;
  32. ctx->avctx = avctx;
  33. ctx->ch = av_mallocz_array(sizeof(ctx->ch[0]), avctx->channels * 2);
  34. ctx->group = av_mallocz_array(sizeof(ctx->group[0]), num_groups);
  35. ctx->bands = av_malloc_array (sizeof(ctx->bands[0]), num_lens);
  36. ctx->num_bands = av_malloc_array (sizeof(ctx->num_bands[0]), num_lens);
  37. ctx->cutoff = avctx->cutoff;
  38. if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
  39. ff_psy_end(ctx);
  40. return AVERROR(ENOMEM);
  41. }
  42. memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
  43. memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
  44. /* assign channels to groups (with virtual channels for coupling) */
  45. for (i = 0; i < num_groups; i++) {
  46. /* NOTE: Add 1 to handle the AAC chan_config without modification.
  47. * This has the side effect of allowing an array of 0s to map
  48. * to one channel per group.
  49. */
  50. ctx->group[i].num_ch = group_map[i] + 1;
  51. for (j = 0; j < ctx->group[i].num_ch * 2; j++)
  52. ctx->group[i].ch[j] = &ctx->ch[k++];
  53. }
  54. switch (ctx->avctx->codec_id) {
  55. case AV_CODEC_ID_AAC:
  56. ctx->model = &ff_aac_psy_model;
  57. break;
  58. }
  59. if (ctx->model->init)
  60. return ctx->model->init(ctx);
  61. return 0;
  62. }
  63. FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel)
  64. {
  65. int i = 0, ch = 0;
  66. while (ch <= channel)
  67. ch += ctx->group[i++].num_ch;
  68. return &ctx->group[i-1];
  69. }
  70. av_cold void ff_psy_end(FFPsyContext *ctx)
  71. {
  72. if (ctx->model && ctx->model->end)
  73. ctx->model->end(ctx);
  74. av_freep(&ctx->bands);
  75. av_freep(&ctx->num_bands);
  76. av_freep(&ctx->group);
  77. av_freep(&ctx->ch);
  78. }
  79. typedef struct FFPsyPreprocessContext{
  80. AVCodecContext *avctx;
  81. float stereo_att;
  82. struct FFIIRFilterCoeffs *fcoeffs;
  83. struct FFIIRFilterState **fstate;
  84. struct FFIIRFilterContext fiir;
  85. }FFPsyPreprocessContext;
  86. #define FILT_ORDER 4
  87. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
  88. {
  89. FFPsyPreprocessContext *ctx;
  90. int i;
  91. float cutoff_coeff = 0;
  92. ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
  93. if (!ctx)
  94. return NULL;
  95. ctx->avctx = avctx;
  96. /* AAC has its own LP method */
  97. if (avctx->codec_id != AV_CODEC_ID_AAC) {
  98. if (avctx->cutoff > 0)
  99. cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
  100. if (cutoff_coeff && cutoff_coeff < 0.98)
  101. ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
  102. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  103. cutoff_coeff, 0.0, 0.0);
  104. if (ctx->fcoeffs) {
  105. ctx->fstate = av_mallocz_array(sizeof(ctx->fstate[0]), avctx->channels);
  106. if (!ctx->fstate) {
  107. av_free(ctx->fcoeffs);
  108. av_free(ctx);
  109. return NULL;
  110. }
  111. for (i = 0; i < avctx->channels; i++)
  112. ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
  113. }
  114. }
  115. ff_iir_filter_init(&ctx->fiir);
  116. return ctx;
  117. }
  118. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
  119. {
  120. int ch;
  121. int frame_size = ctx->avctx->frame_size;
  122. FFIIRFilterContext *iir = &ctx->fiir;
  123. if (ctx->fstate) {
  124. for (ch = 0; ch < channels; ch++)
  125. iir->filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
  126. &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
  127. }
  128. }
  129. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
  130. {
  131. int i;
  132. ff_iir_filter_free_coeffsp(&ctx->fcoeffs);
  133. if (ctx->fstate)
  134. for (i = 0; i < ctx->avctx->channels; i++)
  135. ff_iir_filter_free_statep(&ctx->fstate[i]);
  136. av_freep(&ctx->fstate);
  137. av_free(ctx);
  138. }