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.

211 lines
6.2KB

  1. /*
  2. * iLBC decoder/encoder stub
  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 <ilbc.h>
  22. #include "avcodec.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "internal.h"
  26. static int get_mode(AVCodecContext *avctx)
  27. {
  28. if (avctx->block_align == 38)
  29. return 20;
  30. else if (avctx->block_align == 50)
  31. return 30;
  32. else if (avctx->bit_rate > 0)
  33. return avctx->bit_rate <= 14000 ? 30 : 20;
  34. else
  35. return -1;
  36. }
  37. typedef struct ILBCDecContext {
  38. const AVClass *class;
  39. AVFrame frame;
  40. iLBC_Dec_Inst_t decoder;
  41. int enhance;
  42. } ILBCDecContext;
  43. static const AVOption ilbc_dec_options[] = {
  44. { "enhance", "Enhance the decoded audio (adds delay)", offsetof(ILBCDecContext, enhance), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
  45. { NULL }
  46. };
  47. static const AVClass ilbc_dec_class = {
  48. "libilbc", av_default_item_name, ilbc_dec_options, LIBAVUTIL_VERSION_INT
  49. };
  50. static av_cold int ilbc_decode_init(AVCodecContext *avctx)
  51. {
  52. ILBCDecContext *s = avctx->priv_data;
  53. int mode;
  54. if ((mode = get_mode(avctx)) < 0) {
  55. av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
  56. return AVERROR(EINVAL);
  57. }
  58. WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);
  59. avcodec_get_frame_defaults(&s->frame);
  60. avctx->coded_frame = &s->frame;
  61. avctx->channels = 1;
  62. avctx->sample_rate = 8000;
  63. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  64. return 0;
  65. }
  66. static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
  67. int *got_frame_ptr, AVPacket *avpkt)
  68. {
  69. const uint8_t *buf = avpkt->data;
  70. int buf_size = avpkt->size;
  71. ILBCDecContext *s = avctx->priv_data;
  72. int ret;
  73. if (s->decoder.no_of_bytes > buf_size) {
  74. av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
  75. buf_size, s->decoder.no_of_bytes);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. s->frame.nb_samples = s->decoder.blockl;
  79. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  80. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  81. return ret;
  82. }
  83. WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) s->frame.data[0],
  84. (const WebRtc_UWord16*) buf, &s->decoder, 1);
  85. *got_frame_ptr = 1;
  86. *(AVFrame *)data = s->frame;
  87. return s->decoder.no_of_bytes;
  88. }
  89. AVCodec ff_libilbc_decoder = {
  90. .name = "libilbc",
  91. .type = AVMEDIA_TYPE_AUDIO,
  92. .id = AV_CODEC_ID_ILBC,
  93. .priv_data_size = sizeof(ILBCDecContext),
  94. .init = ilbc_decode_init,
  95. .decode = ilbc_decode_frame,
  96. .capabilities = CODEC_CAP_DR1,
  97. .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
  98. .priv_class = &ilbc_dec_class,
  99. };
  100. typedef struct ILBCEncContext {
  101. const AVClass *class;
  102. iLBC_Enc_Inst_t encoder;
  103. int mode;
  104. } ILBCEncContext;
  105. static const AVOption ilbc_enc_options[] = {
  106. { "mode", "iLBC mode (20 or 30 ms frames)", offsetof(ILBCEncContext, mode), AV_OPT_TYPE_INT, { .i64 = 20 }, 20, 30, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  107. { NULL }
  108. };
  109. static const AVClass ilbc_enc_class = {
  110. "libilbc", av_default_item_name, ilbc_enc_options, LIBAVUTIL_VERSION_INT
  111. };
  112. static av_cold int ilbc_encode_init(AVCodecContext *avctx)
  113. {
  114. ILBCEncContext *s = avctx->priv_data;
  115. int mode;
  116. if (avctx->sample_rate != 8000) {
  117. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  118. return AVERROR(EINVAL);
  119. }
  120. if (avctx->channels != 1) {
  121. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  122. return AVERROR(EINVAL);
  123. }
  124. if ((mode = get_mode(avctx)) > 0)
  125. s->mode = mode;
  126. else
  127. s->mode = s->mode != 30 ? 20 : 30;
  128. WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);
  129. avctx->block_align = s->encoder.no_of_bytes;
  130. avctx->frame_size = s->encoder.blockl;
  131. #if FF_API_OLD_ENCODE_AUDIO
  132. avctx->coded_frame = avcodec_alloc_frame();
  133. if (!avctx->coded_frame)
  134. return AVERROR(ENOMEM);
  135. #endif
  136. return 0;
  137. }
  138. static av_cold int ilbc_encode_close(AVCodecContext *avctx)
  139. {
  140. #if FF_API_OLD_ENCODE_AUDIO
  141. av_freep(&avctx->coded_frame);
  142. #endif
  143. return 0;
  144. }
  145. static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  146. const AVFrame *frame, int *got_packet_ptr)
  147. {
  148. ILBCEncContext *s = avctx->priv_data;
  149. int ret;
  150. if ((ret = ff_alloc_packet(avpkt, 50))) {
  151. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  152. return ret;
  153. }
  154. WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
  155. avpkt->size = s->encoder.no_of_bytes;
  156. *got_packet_ptr = 1;
  157. return 0;
  158. }
  159. static const AVCodecDefault ilbc_encode_defaults[] = {
  160. { "b", "0" },
  161. { NULL }
  162. };
  163. AVCodec ff_libilbc_encoder = {
  164. .name = "libilbc",
  165. .type = AVMEDIA_TYPE_AUDIO,
  166. .id = AV_CODEC_ID_ILBC,
  167. .priv_data_size = sizeof(ILBCEncContext),
  168. .init = ilbc_encode_init,
  169. .encode2 = ilbc_encode_frame,
  170. .close = ilbc_encode_close,
  171. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  172. AV_SAMPLE_FMT_NONE },
  173. .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
  174. .defaults = ilbc_encode_defaults,
  175. .priv_class = &ilbc_enc_class,
  176. };