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.

215 lines
6.2KB

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