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.

174 lines
5.5KB

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