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.

217 lines
6.3KB

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