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.

179 lines
5.6KB

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