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.

137 lines
4.5KB

  1. /*
  2. * Xiph CELT / Opus decoder using libcelt
  3. * Copyright (c) 2011 Nicolas George
  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 <celt/celt.h>
  22. #include <celt/celt_header.h>
  23. #include "avcodec.h"
  24. #include "libavutil/intreadwrite.h"
  25. struct libcelt_context {
  26. CELTMode *mode;
  27. CELTDecoder *dec;
  28. int frame_bytes;
  29. int discard;
  30. };
  31. static int ff_celt_error_to_averror(int err)
  32. {
  33. switch(err) {
  34. case CELT_BAD_ARG: return AVERROR(EINVAL);
  35. #ifdef CELT_BUFFER_TOO_SMALL
  36. case CELT_BUFFER_TOO_SMALL: return AVERROR(ENOBUFS);
  37. #endif
  38. case CELT_INTERNAL_ERROR: return AVERROR(EFAULT);
  39. case CELT_CORRUPTED_DATA: return AVERROR_INVALIDDATA;
  40. case CELT_UNIMPLEMENTED: return AVERROR(ENOTSUP);
  41. #ifdef ENOTRECOVERABLE
  42. case CELT_INVALID_STATE: return AVERROR(ENOTRECOVERABLE);
  43. #endif
  44. case CELT_ALLOC_FAIL: return AVERROR(ENOMEM);
  45. default: return AVERROR(EINVAL);
  46. }
  47. }
  48. static int ff_celt_bitstream_version_hack(CELTMode *mode)
  49. {
  50. CELTHeader header = { .version_id = 0 };
  51. celt_header_init(&header, mode, 960, 2);
  52. return header.version_id;
  53. }
  54. static av_cold int libcelt_dec_init(AVCodecContext *c)
  55. {
  56. struct libcelt_context *celt = c->priv_data;
  57. int err;
  58. if (!c->channels || !c->frame_size ||
  59. c->frame_size > INT_MAX / sizeof(int16_t) / c->channels)
  60. return AVERROR(EINVAL);
  61. celt->frame_bytes = c->frame_size * c->channels * sizeof(int16_t);
  62. celt->mode = celt_mode_create(c->sample_rate, c->frame_size, &err);
  63. if (!celt->mode)
  64. return ff_celt_error_to_averror(err);
  65. celt->dec = celt_decoder_create_custom(celt->mode, c->channels, &err);
  66. if (!celt->dec) {
  67. celt_mode_destroy(celt->mode);
  68. return ff_celt_error_to_averror(err);
  69. }
  70. if (c->extradata_size >= 4) {
  71. celt->discard = AV_RL32(c->extradata);
  72. if (celt->discard < 0 || celt->discard >= c->frame_size) {
  73. av_log(c, AV_LOG_WARNING,
  74. "Invalid overlap (%d), ignored.\n", celt->discard);
  75. celt->discard = 0;
  76. }
  77. celt->discard *= c->channels * sizeof(int16_t);
  78. }
  79. if(c->extradata_size >= 8) {
  80. unsigned version = AV_RL32(c->extradata + 4);
  81. unsigned lib_version = ff_celt_bitstream_version_hack(celt->mode);
  82. if (version != lib_version)
  83. av_log(c, AV_LOG_WARNING,
  84. "CELT bitstream version 0x%x may be "
  85. "improperly decoded by libcelt for version 0x%x.\n",
  86. version, lib_version);
  87. }
  88. return 0;
  89. }
  90. static av_cold int libcelt_dec_close(AVCodecContext *c)
  91. {
  92. struct libcelt_context *celt = c->priv_data;
  93. celt_decoder_destroy(celt->dec);
  94. celt_mode_destroy(celt->mode);
  95. return 0;
  96. }
  97. static int libcelt_dec_decode(AVCodecContext *c, void *pcm, int *pcm_size,
  98. AVPacket *pkt)
  99. {
  100. struct libcelt_context *celt = c->priv_data;
  101. int err;
  102. if (*pcm_size < celt->frame_bytes)
  103. return AVERROR(ENOBUFS);
  104. err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
  105. if (err < 0)
  106. return ff_celt_error_to_averror(err);
  107. *pcm_size = celt->frame_bytes;
  108. if (celt->discard) {
  109. *pcm_size = celt->frame_bytes - celt->discard;
  110. memmove(pcm, (char *)pcm + celt->discard, *pcm_size);
  111. celt->discard = 0;
  112. }
  113. return pkt->size;
  114. }
  115. AVCodec ff_libcelt_decoder = {
  116. .name = "libcelt",
  117. .type = AVMEDIA_TYPE_AUDIO,
  118. .id = CODEC_ID_CELT,
  119. .priv_data_size = sizeof(struct libcelt_context),
  120. .init = libcelt_dec_init,
  121. .close = libcelt_dec_close,
  122. .decode = libcelt_dec_decode,
  123. .capabilities = 0,
  124. .long_name = NULL_IF_CONFIG_SMALL("Xiph CELT/Opus decoder using libcelt"),
  125. };