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.

178 lines
5.6KB

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