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.

219 lines
6.4KB

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