libvo-amrwbenc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. static const char wb_bitrate_unsupported[] =
  24. "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, "
  25. "18.25k, 19.85k, 23.05k, or 23.85k\n";
  26. typedef struct AMRWB_bitrates {
  27. int rate;
  28. int mode;
  29. } AMRWB_bitrates;
  30. typedef struct AMRWBContext {
  31. void *state;
  32. int mode;
  33. int allow_dtx;
  34. } AMRWBContext;
  35. static int getWBBitrateMode(int bitrate)
  36. {
  37. /* make the correspondance between bitrate and mode */
  38. AMRWB_bitrates rates[] = { { 6600, 0},
  39. { 8850, 1},
  40. {12650, 2},
  41. {14250, 3},
  42. {15850, 4},
  43. {18250, 5},
  44. {19850, 6},
  45. {23050, 7},
  46. {23850, 8}, };
  47. int i;
  48. for (i = 0; i < 9; i++)
  49. if (rates[i].rate == bitrate)
  50. return rates[i].mode;
  51. /* no bitrate matching, return an error */
  52. return -1;
  53. }
  54. static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
  55. {
  56. AMRWBContext *s = avctx->priv_data;
  57. if (avctx->sample_rate != 16000) {
  58. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  59. return -1;
  60. }
  61. if (avctx->channels != 1) {
  62. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  63. return -1;
  64. }
  65. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  66. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  67. return -1;
  68. }
  69. avctx->frame_size = 320;
  70. avctx->coded_frame = avcodec_alloc_frame();
  71. s->state = E_IF_init();
  72. s->allow_dtx = 0;
  73. return 0;
  74. }
  75. static int amr_wb_encode_close(AVCodecContext *avctx)
  76. {
  77. AMRWBContext *s = avctx->priv_data;
  78. E_IF_exit(s->state);
  79. av_freep(&avctx->coded_frame);
  80. return 0;
  81. }
  82. static int amr_wb_encode_frame(AVCodecContext *avctx,
  83. unsigned char *frame/*out*/,
  84. int buf_size, void *data/*in*/)
  85. {
  86. AMRWBContext *s = avctx->priv_data;
  87. int size;
  88. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  89. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  90. return -1;
  91. }
  92. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  93. return size;
  94. }
  95. AVCodec ff_libvo_amrwbenc_encoder = {
  96. "libvo_amrwbenc",
  97. CODEC_TYPE_AUDIO,
  98. CODEC_ID_AMR_WB,
  99. sizeof(AMRWBContext),
  100. amr_wb_encode_init,
  101. amr_wb_encode_frame,
  102. amr_wb_encode_close,
  103. NULL,
  104. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  105. .long_name = NULL_IF_CONFIG_SMALL("libvo-amrwbenc Adaptive Multi-Rate "
  106. "(AMR) Wide-Band"),
  107. };