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.

152 lines
4.7KB

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