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.

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