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.

163 lines
5.2KB

  1. /*
  2. * RFC 3389 comfort noise generator
  3. * Copyright (c) 2012 Martin Storsjo
  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 <math.h>
  22. #include "libavutil/common.h"
  23. #include "avcodec.h"
  24. #include "celp_filters.h"
  25. #include "libavutil/lfg.h"
  26. typedef struct CNGContext {
  27. AVFrame avframe;
  28. float *refl_coef, *target_refl_coef;
  29. float *lpc_coef;
  30. int order;
  31. int energy, target_energy;
  32. float *filter_out;
  33. float *excitation;
  34. AVLFG lfg;
  35. } CNGContext;
  36. static av_cold int cng_decode_close(AVCodecContext *avctx)
  37. {
  38. CNGContext *p = avctx->priv_data;
  39. av_free(p->refl_coef);
  40. av_free(p->target_refl_coef);
  41. av_free(p->lpc_coef);
  42. av_free(p->filter_out);
  43. av_free(p->excitation);
  44. return 0;
  45. }
  46. static av_cold int cng_decode_init(AVCodecContext *avctx)
  47. {
  48. CNGContext *p = avctx->priv_data;
  49. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  50. avctx->channels = 1;
  51. avctx->sample_rate = 8000;
  52. avcodec_get_frame_defaults(&p->avframe);
  53. avctx->coded_frame = &p->avframe;
  54. p->order = 12;
  55. avctx->frame_size = 640;
  56. p->refl_coef = av_mallocz(p->order * sizeof(*p->refl_coef));
  57. p->target_refl_coef = av_mallocz(p->order * sizeof(*p->target_refl_coef));
  58. p->lpc_coef = av_mallocz(p->order * sizeof(*p->lpc_coef));
  59. p->filter_out = av_mallocz((avctx->frame_size + p->order) *
  60. sizeof(*p->filter_out));
  61. p->excitation = av_mallocz(avctx->frame_size * sizeof(*p->excitation));
  62. if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
  63. !p->filter_out || !p->excitation) {
  64. cng_decode_close(avctx);
  65. return AVERROR(ENOMEM);
  66. }
  67. av_lfg_init(&p->lfg, 0);
  68. return 0;
  69. }
  70. static void make_lpc_coefs(float *lpc, const float *refl, int order)
  71. {
  72. float buf[100];
  73. float *next, *cur;
  74. int m, i;
  75. next = buf;
  76. cur = lpc;
  77. for (m = 0; m < order; m++) {
  78. next[m] = refl[m];
  79. for (i = 0; i < m; i++)
  80. next[i] = cur[i] + refl[m] * cur[m - i - 1];
  81. FFSWAP(float*, next, cur);
  82. }
  83. if (cur != lpc)
  84. memcpy(lpc, cur, sizeof(*lpc) * order);
  85. }
  86. static int cng_decode_frame(AVCodecContext *avctx, void *data,
  87. int *got_frame_ptr, AVPacket *avpkt)
  88. {
  89. CNGContext *p = avctx->priv_data;
  90. int buf_size = avpkt->size;
  91. int ret, i;
  92. int16_t *buf_out;
  93. float e = 1.0;
  94. float scaling;
  95. if (avpkt->size) {
  96. float dbov = -avpkt->data[0] / 10.0;
  97. p->target_energy = 1081109975 * pow(10, dbov) * 0.75;
  98. memset(p->target_refl_coef, 0, sizeof(p->refl_coef));
  99. for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
  100. p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
  101. }
  102. make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
  103. }
  104. p->energy = p->energy / 2 + p->target_energy / 2;
  105. for (i = 0; i < p->order; i++)
  106. p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
  107. for (i = 0; i < p->order; i++)
  108. e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
  109. scaling = sqrt(e * p->energy / 1081109975);
  110. for (i = 0; i < avctx->frame_size; i++) {
  111. int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
  112. p->excitation[i] = scaling * r;
  113. }
  114. ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
  115. p->excitation, avctx->frame_size, p->order);
  116. p->avframe.nb_samples = avctx->frame_size;
  117. if ((ret = avctx->get_buffer(avctx, &p->avframe)) < 0) {
  118. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  119. return ret;
  120. }
  121. buf_out = (int16_t *)p->avframe.data[0];
  122. for (i = 0; i < avctx->frame_size; i++)
  123. buf_out[i] = p->filter_out[i + p->order];
  124. memcpy(p->filter_out, p->filter_out + avctx->frame_size,
  125. p->order * sizeof(*p->filter_out));
  126. *got_frame_ptr = 1;
  127. *(AVFrame *)data = p->avframe;
  128. return buf_size;
  129. }
  130. AVCodec ff_comfortnoise_decoder = {
  131. .name = "comfortnoise",
  132. .type = AVMEDIA_TYPE_AUDIO,
  133. .id = AV_CODEC_ID_COMFORT_NOISE,
  134. .priv_data_size = sizeof(CNGContext),
  135. .init = cng_decode_init,
  136. .decode = cng_decode_frame,
  137. .close = cng_decode_close,
  138. .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
  139. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  140. AV_SAMPLE_FMT_NONE },
  141. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
  142. };