libvo-amrwbenc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * AMR Audio encoder stub
  3. * Copyright (c) 2003 the ffmpeg project
  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 <vo-amrwbenc/enc_if.h>
  22. #include "avcodec.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/opt.h"
  25. typedef struct AMRWBContext {
  26. AVClass *av_class;
  27. void *state;
  28. int mode;
  29. int last_bitrate;
  30. int allow_dtx;
  31. } AMRWBContext;
  32. static const AVOption options[] = {
  33. { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRWBContext, allow_dtx), FF_OPT_TYPE_INT, 0, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  34. { NULL }
  35. };
  36. static const AVClass class = {
  37. "libvo_amrwbenc", av_default_item_name, options, LIBAVUTIL_VERSION_INT
  38. };
  39. static int get_wb_bitrate_mode(int bitrate, void *log_ctx)
  40. {
  41. /* make the correspondance between bitrate and mode */
  42. static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250,
  43. 19850, 23050, 23850 };
  44. int i, best = -1, min_diff = 0;
  45. char log_buf[200];
  46. for (i = 0; i < 9; i++) {
  47. if (rates[i] == bitrate)
  48. return i;
  49. if (best < 0 || abs(rates[i] - bitrate) < min_diff) {
  50. best = i;
  51. min_diff = abs(rates[i] - bitrate);
  52. }
  53. }
  54. /* no bitrate matching exactly, log a warning */
  55. snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of ");
  56. for (i = 0; i < 9; i++)
  57. av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i] / 1000.f);
  58. av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best] / 1000.f);
  59. av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf);
  60. return best;
  61. }
  62. static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
  63. {
  64. AMRWBContext *s = avctx->priv_data;
  65. if (avctx->sample_rate != 16000) {
  66. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  67. return AVERROR(ENOSYS);
  68. }
  69. if (avctx->channels != 1) {
  70. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  71. return AVERROR(ENOSYS);
  72. }
  73. s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
  74. s->last_bitrate = avctx->bit_rate;
  75. avctx->frame_size = 320;
  76. avctx->coded_frame = avcodec_alloc_frame();
  77. s->state = E_IF_init();
  78. return 0;
  79. }
  80. static int amr_wb_encode_close(AVCodecContext *avctx)
  81. {
  82. AMRWBContext *s = avctx->priv_data;
  83. E_IF_exit(s->state);
  84. av_freep(&avctx->coded_frame);
  85. return 0;
  86. }
  87. static int amr_wb_encode_frame(AVCodecContext *avctx,
  88. unsigned char *frame/*out*/,
  89. int buf_size, void *data/*in*/)
  90. {
  91. AMRWBContext *s = avctx->priv_data;
  92. int size;
  93. if (s->last_bitrate != avctx->bit_rate) {
  94. s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
  95. s->last_bitrate = avctx->bit_rate;
  96. }
  97. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  98. return size;
  99. }
  100. AVCodec ff_libvo_amrwbenc_encoder = {
  101. "libvo_amrwbenc",
  102. AVMEDIA_TYPE_AUDIO,
  103. CODEC_ID_AMR_WB,
  104. sizeof(AMRWBContext),
  105. amr_wb_encode_init,
  106. amr_wb_encode_frame,
  107. amr_wb_encode_close,
  108. NULL,
  109. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  110. .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn Adaptive Multi-Rate "
  111. "(AMR) Wide-Band"),
  112. .priv_class = &class,
  113. };