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.

117 lines
3.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 "internal.h"
  25. #include "lpc.h"
  26. typedef struct CNGContext {
  27. LPCContext lpc;
  28. int order;
  29. int32_t *samples32;
  30. double *ref_coef;
  31. } CNGContext;
  32. static av_cold int cng_encode_close(AVCodecContext *avctx)
  33. {
  34. CNGContext *p = avctx->priv_data;
  35. ff_lpc_end(&p->lpc);
  36. av_free(p->samples32);
  37. av_free(p->ref_coef);
  38. return 0;
  39. }
  40. static av_cold int cng_encode_init(AVCodecContext *avctx)
  41. {
  42. CNGContext *p = avctx->priv_data;
  43. int ret;
  44. if (avctx->channels != 1) {
  45. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  46. return AVERROR(EINVAL);
  47. }
  48. avctx->frame_size = 640;
  49. p->order = 10;
  50. if ((ret = ff_lpc_init(&p->lpc, avctx->frame_size, p->order, FF_LPC_TYPE_LEVINSON)) < 0)
  51. return ret;
  52. p->samples32 = av_malloc(avctx->frame_size * sizeof(*p->samples32));
  53. p->ref_coef = av_malloc(p->order * sizeof(*p->ref_coef));
  54. if (!p->samples32 || !p->ref_coef) {
  55. cng_encode_close(avctx);
  56. return AVERROR(ENOMEM);
  57. }
  58. return 0;
  59. }
  60. static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  61. const AVFrame *frame, int *got_packet_ptr)
  62. {
  63. CNGContext *p = avctx->priv_data;
  64. int ret, i;
  65. double energy = 0;
  66. int qdbov;
  67. int16_t *samples = (int16_t*) frame->data[0];
  68. if ((ret = ff_alloc_packet(avpkt, 1 + p->order))) {
  69. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  70. return ret;
  71. }
  72. for (i = 0; i < frame->nb_samples; i++) {
  73. p->samples32[i] = samples[i];
  74. energy += samples[i] * samples[i];
  75. }
  76. energy /= frame->nb_samples;
  77. if (energy > 0) {
  78. double dbov = 10 * log10(energy / 1081109975);
  79. qdbov = av_clip(-floor(dbov), 0, 127);
  80. } else {
  81. qdbov = 127;
  82. }
  83. ret = ff_lpc_calc_ref_coefs(&p->lpc, p->samples32, p->order, p->ref_coef);
  84. avpkt->data[0] = qdbov;
  85. for (i = 0; i < p->order; i++)
  86. avpkt->data[1 + i] = p->ref_coef[i] * 127 + 127;
  87. *got_packet_ptr = 1;
  88. avpkt->size = 1 + p->order;
  89. return 0;
  90. }
  91. AVCodec ff_comfortnoise_encoder = {
  92. .name = "comfortnoise",
  93. .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
  94. .type = AVMEDIA_TYPE_AUDIO,
  95. .id = AV_CODEC_ID_COMFORT_NOISE,
  96. .priv_data_size = sizeof(CNGContext),
  97. .init = cng_encode_init,
  98. .encode2 = cng_encode_frame,
  99. .close = cng_encode_close,
  100. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  101. AV_SAMPLE_FMT_NONE },
  102. };