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.

214 lines
7.2KB

  1. /*
  2. * codec2 encoder/decoder using libcodec2
  3. * Copyright (c) 2017 Tomas Härdin
  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 <codec2/codec2.h>
  22. #include "avcodec.h"
  23. #include "libavutil/opt.h"
  24. #include "internal.h"
  25. #include "codec2utils.h"
  26. typedef struct {
  27. const AVClass *class;
  28. struct CODEC2 *codec;
  29. int mode;
  30. } LibCodec2Context;
  31. static const AVOption options[] = {
  32. //not AV_OPT_FLAG_DECODING_PARAM since mode should come from the demuxer
  33. //1300 (aka FreeDV 1600) is the most common mode on-the-air, default to it here as well
  34. AVPRIV_CODEC2_AVOPTIONS("codec2 mode", LibCodec2Context, 0, 4 /*CODEC2_MODE_1300*/, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM),
  35. { NULL },
  36. };
  37. static const AVClass libcodec2_enc_class = {
  38. .class_name = "libcodec2 encoder",
  39. .item_name = av_default_item_name,
  40. .option = options,
  41. .version = LIBAVUTIL_VERSION_INT,
  42. };
  43. static const AVClass libcodec2_dec_class = {
  44. .class_name = "libcodec2 decoder",
  45. .item_name = av_default_item_name,
  46. .version = LIBAVUTIL_VERSION_INT,
  47. };
  48. static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode)
  49. {
  50. LibCodec2Context *c2 = avctx->priv_data;
  51. //Grab mode name from options, unless it's some weird number.
  52. const char *modename = mode >= 0 && mode <= AVPRIV_CODEC2_MODE_MAX ? options[mode+1].name : "?";
  53. c2->codec = codec2_create(mode);
  54. if (!c2->codec) {
  55. //Out of memory or unsupported mode. The latter seems most likely,
  56. //but we can't tell for sure with the current API.
  57. goto libcodec2_init_common_error;
  58. }
  59. avctx->frame_size = codec2_samples_per_frame(c2->codec);
  60. avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8;
  61. if (avctx->frame_size <= 0 || avctx->block_align <= 0) {
  62. //codec2_create() may succeed for some modes but still fail at codec2_samples_per_frame()
  63. //example is -mode 700C on libcodec2 0.4
  64. codec2_destroy(c2->codec);
  65. c2->codec = NULL;
  66. goto libcodec2_init_common_error;
  67. }
  68. codec2_set_natural_or_gray(c2->codec, 1);
  69. return 0;
  70. libcodec2_init_common_error:
  71. av_log(avctx, AV_LOG_ERROR,
  72. "Mode %i (%s) not supported with the linked version of libcodec2\n",
  73. mode, modename);
  74. return AVERROR(EINVAL);
  75. }
  76. static av_cold int libcodec2_init_decoder(AVCodecContext *avctx)
  77. {
  78. avctx->sample_rate = 8000;
  79. avctx->channels = 1;
  80. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  81. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  82. if (avctx->extradata_size != AVPRIV_CODEC2_EXTRADATA_SIZE) {
  83. av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n",
  84. AVPRIV_CODEC2_EXTRADATA_SIZE, avctx->extradata_size);
  85. return AVERROR_INVALIDDATA;
  86. }
  87. return libcodec2_init_common(avctx, avpriv_codec2_mode_from_extradata(avctx->extradata));
  88. }
  89. static av_cold int libcodec2_init_encoder(AVCodecContext *avctx)
  90. {
  91. LibCodec2Context *c2 = avctx->priv_data;
  92. //will need to be smarter once we get wideband support
  93. if (avctx->sample_rate != 8000 ||
  94. avctx->channels != 1 ||
  95. avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
  96. av_log(avctx, AV_LOG_ERROR, "only 8 kHz 16-bit mono allowed\n");
  97. return AVERROR(EINVAL);
  98. }
  99. avctx->extradata = av_mallocz(AVPRIV_CODEC2_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
  100. if (!avctx->extradata) {
  101. return AVERROR(ENOMEM);
  102. }
  103. avctx->extradata_size = AVPRIV_CODEC2_EXTRADATA_SIZE;
  104. avpriv_codec2_make_extradata(avctx->extradata, c2->mode);
  105. return libcodec2_init_common(avctx, c2->mode);
  106. }
  107. static av_cold int libcodec2_close(AVCodecContext *avctx)
  108. {
  109. LibCodec2Context *c2 = avctx->priv_data;
  110. codec2_destroy(c2->codec);
  111. return 0;
  112. }
  113. static int libcodec2_decode(AVCodecContext *avctx, void *data,
  114. int *got_frame_ptr, AVPacket *pkt)
  115. {
  116. LibCodec2Context *c2 = avctx->priv_data;
  117. AVFrame *frame = data;
  118. int ret, nframes, i;
  119. uint8_t *input;
  120. int16_t *output;
  121. nframes = pkt->size / avctx->block_align;
  122. frame->nb_samples = avctx->frame_size * nframes;
  123. ret = ff_get_buffer(avctx, frame, 0);
  124. if (ret < 0) {
  125. return ret;
  126. }
  127. input = pkt->data;
  128. output = (int16_t *)frame->data[0];
  129. for (i = 0; i < nframes; i++) {
  130. codec2_decode(c2->codec, output, input);
  131. input += avctx->block_align;
  132. output += avctx->frame_size;
  133. }
  134. *got_frame_ptr = nframes > 0;
  135. return nframes * avctx->block_align;
  136. }
  137. static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
  138. const AVFrame *frame, int *got_packet_ptr)
  139. {
  140. LibCodec2Context *c2 = avctx->priv_data;
  141. int16_t *samples = (int16_t *)frame->data[0];
  142. int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0);
  143. if (ret < 0) {
  144. return ret;
  145. }
  146. codec2_encode(c2->codec, avpkt->data, samples);
  147. *got_packet_ptr = 1;
  148. return 0;
  149. }
  150. AVCodec ff_libcodec2_decoder = {
  151. .name = "libcodec2",
  152. .long_name = NULL_IF_CONFIG_SMALL("codec2 decoder using libcodec2"),
  153. .type = AVMEDIA_TYPE_AUDIO,
  154. .id = AV_CODEC_ID_CODEC2,
  155. .priv_data_size = sizeof(LibCodec2Context),
  156. .init = libcodec2_init_decoder,
  157. .close = libcodec2_close,
  158. .decode = libcodec2_decode,
  159. .capabilities = 0,
  160. .supported_samplerates = (const int[]){ 8000, 0 },
  161. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
  162. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
  163. .priv_class = &libcodec2_dec_class,
  164. };
  165. AVCodec ff_libcodec2_encoder = {
  166. .name = "libcodec2",
  167. .long_name = NULL_IF_CONFIG_SMALL("codec2 encoder using libcodec2"),
  168. .type = AVMEDIA_TYPE_AUDIO,
  169. .id = AV_CODEC_ID_CODEC2,
  170. .priv_data_size = sizeof(LibCodec2Context),
  171. .init = libcodec2_init_encoder,
  172. .close = libcodec2_close,
  173. .encode2 = libcodec2_encode,
  174. .capabilities = 0,
  175. .supported_samplerates = (const int[]){ 8000, 0 },
  176. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
  177. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
  178. .priv_class = &libcodec2_enc_class,
  179. };