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.

143 lines
4.6KB

  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, int num_lens,
  26. const uint8_t **bands, const int* num_bands,
  27. int num_groups, const uint8_t *group_map)
  28. {
  29. int i, j, k = 0;
  30. ctx->avctx = avctx;
  31. ctx->ch = av_mallocz(sizeof(ctx->ch[0]) * avctx->channels * 2);
  32. ctx->group = av_mallocz(sizeof(ctx->group[0]) * num_groups);
  33. ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens);
  34. ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
  35. memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
  36. memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
  37. /* assign channels to groups (with virtual channels for coupling) */
  38. for (i = 0; i < num_groups; i++) {
  39. /* NOTE: Add 1 to handle the AAC chan_config without modification.
  40. * This has the side effect of allowing an array of 0s to map
  41. * to one channel per group.
  42. */
  43. ctx->group[i].num_ch = group_map[i] + 1;
  44. for (j = 0; j < ctx->group[i].num_ch * 2; j++)
  45. ctx->group[i].ch[j] = &ctx->ch[k++];
  46. }
  47. switch (ctx->avctx->codec_id) {
  48. case CODEC_ID_AAC:
  49. ctx->model = &ff_aac_psy_model;
  50. break;
  51. }
  52. if (ctx->model->init)
  53. return ctx->model->init(ctx);
  54. return 0;
  55. }
  56. FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel)
  57. {
  58. int i = 0, ch = 0;
  59. while (ch <= channel)
  60. ch += ctx->group[i++].num_ch;
  61. return &ctx->group[i-1];
  62. }
  63. av_cold void ff_psy_end(FFPsyContext *ctx)
  64. {
  65. if (ctx->model->end)
  66. ctx->model->end(ctx);
  67. av_freep(&ctx->bands);
  68. av_freep(&ctx->num_bands);
  69. av_freep(&ctx->group);
  70. av_freep(&ctx->ch);
  71. }
  72. typedef struct FFPsyPreprocessContext{
  73. AVCodecContext *avctx;
  74. float stereo_att;
  75. struct FFIIRFilterCoeffs *fcoeffs;
  76. struct FFIIRFilterState **fstate;
  77. }FFPsyPreprocessContext;
  78. #define FILT_ORDER 4
  79. av_cold struct FFPsyPreprocessContext* ff_psy_preprocess_init(AVCodecContext *avctx)
  80. {
  81. FFPsyPreprocessContext *ctx;
  82. int i;
  83. float cutoff_coeff = 0;
  84. ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
  85. ctx->avctx = avctx;
  86. if (avctx->cutoff > 0)
  87. cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
  88. if (cutoff_coeff)
  89. ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
  90. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  91. cutoff_coeff, 0.0, 0.0);
  92. if (ctx->fcoeffs) {
  93. ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
  94. for (i = 0; i < avctx->channels; i++)
  95. ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
  96. }
  97. return ctx;
  98. }
  99. void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx,
  100. const int16_t *audio, int16_t *dest,
  101. int tag, int channels)
  102. {
  103. int ch, i;
  104. if (ctx->fstate) {
  105. for (ch = 0; ch < channels; ch++)
  106. ff_iir_filter(ctx->fcoeffs, ctx->fstate[tag+ch], ctx->avctx->frame_size,
  107. audio + ch, ctx->avctx->channels,
  108. dest + ch, ctx->avctx->channels);
  109. } else {
  110. for (ch = 0; ch < channels; ch++)
  111. for (i = 0; i < ctx->avctx->frame_size; i++)
  112. dest[i*ctx->avctx->channels + ch] = audio[i*ctx->avctx->channels + ch];
  113. }
  114. }
  115. av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
  116. {
  117. int i;
  118. ff_iir_filter_free_coeffs(ctx->fcoeffs);
  119. if (ctx->fstate)
  120. for (i = 0; i < ctx->avctx->channels; i++)
  121. ff_iir_filter_free_state(ctx->fstate[i]);
  122. av_freep(&ctx->fstate);
  123. av_free(ctx);
  124. }