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.

181 lines
5.9KB

  1. /*
  2. * Copyright (C) 2008 David Conrad
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <speex/speex.h>
  21. #include <speex/speex_header.h>
  22. #include <speex/speex_stereo.h>
  23. #include <speex/speex_callbacks.h>
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/common.h"
  26. #include "avcodec.h"
  27. typedef struct {
  28. AVFrame frame;
  29. SpeexBits bits;
  30. SpeexStereoState stereo;
  31. void *dec_state;
  32. int frame_size;
  33. } LibSpeexContext;
  34. static av_cold int libspeex_decode_init(AVCodecContext *avctx)
  35. {
  36. LibSpeexContext *s = avctx->priv_data;
  37. const SpeexMode *mode;
  38. int spx_mode;
  39. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  40. if (avctx->extradata && avctx->extradata_size >= 80) {
  41. SpeexHeader *header = speex_packet_to_header(avctx->extradata,
  42. avctx->extradata_size);
  43. avctx->channels = header->nb_channels;
  44. spx_mode = header->mode;
  45. speex_header_free(header);
  46. } else {
  47. switch (avctx->sample_rate) {
  48. case 8000: spx_mode = 0; break;
  49. case 16000: spx_mode = 1; break;
  50. case 32000: spx_mode = 2; break;
  51. default:
  52. /* libspeex can handle any mode if initialized as ultra-wideband */
  53. av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
  54. "Decoding as 32kHz ultra-wideband\n",
  55. avctx->sample_rate);
  56. spx_mode = 2;
  57. }
  58. }
  59. mode = speex_lib_get_mode(spx_mode);
  60. if (!mode) {
  61. av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
  62. return AVERROR_INVALIDDATA;
  63. }
  64. avctx->sample_rate = 8000 << spx_mode;
  65. s->frame_size = 160 << spx_mode;
  66. if (avctx->channels < 1 || avctx->channels > 2) {
  67. /* libspeex can handle mono or stereo if initialized as stereo */
  68. av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
  69. "Decoding as stereo.\n", avctx->channels);
  70. avctx->channels = 2;
  71. }
  72. avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
  73. AV_CH_LAYOUT_MONO;
  74. speex_bits_init(&s->bits);
  75. s->dec_state = speex_decoder_init(mode);
  76. if (!s->dec_state) {
  77. av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
  78. return -1;
  79. }
  80. if (avctx->channels == 2) {
  81. SpeexCallback callback;
  82. callback.callback_id = SPEEX_INBAND_STEREO;
  83. callback.func = speex_std_stereo_request_handler;
  84. callback.data = &s->stereo;
  85. s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
  86. speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
  87. }
  88. avcodec_get_frame_defaults(&s->frame);
  89. avctx->coded_frame = &s->frame;
  90. return 0;
  91. }
  92. static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
  93. int *got_frame_ptr, AVPacket *avpkt)
  94. {
  95. uint8_t *buf = avpkt->data;
  96. int buf_size = avpkt->size;
  97. LibSpeexContext *s = avctx->priv_data;
  98. int16_t *output;
  99. int ret, consumed = 0;
  100. /* get output buffer */
  101. s->frame.nb_samples = s->frame_size;
  102. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  103. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  104. return ret;
  105. }
  106. output = (int16_t *)s->frame.data[0];
  107. /* if there is not enough data left for the smallest possible frame or the
  108. next 5 bits are a terminator code, reset the libspeex buffer using the
  109. current packet, otherwise ignore the current packet and keep decoding
  110. frames from the libspeex buffer. */
  111. if (speex_bits_remaining(&s->bits) < 5 ||
  112. speex_bits_peek_unsigned(&s->bits, 5) == 0x1F) {
  113. /* check for flush packet */
  114. if (!buf || !buf_size) {
  115. *got_frame_ptr = 0;
  116. return buf_size;
  117. }
  118. /* set new buffer */
  119. speex_bits_read_from(&s->bits, buf, buf_size);
  120. consumed = buf_size;
  121. }
  122. /* decode a single frame */
  123. ret = speex_decode_int(s->dec_state, &s->bits, output);
  124. if (ret <= -2) {
  125. av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
  126. return AVERROR_INVALIDDATA;
  127. }
  128. if (avctx->channels == 2)
  129. speex_decode_stereo_int(output, s->frame_size, &s->stereo);
  130. *got_frame_ptr = 1;
  131. *(AVFrame *)data = s->frame;
  132. return consumed;
  133. }
  134. static av_cold int libspeex_decode_close(AVCodecContext *avctx)
  135. {
  136. LibSpeexContext *s = avctx->priv_data;
  137. speex_bits_destroy(&s->bits);
  138. speex_decoder_destroy(s->dec_state);
  139. return 0;
  140. }
  141. static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
  142. {
  143. LibSpeexContext *s = avctx->priv_data;
  144. speex_bits_reset(&s->bits);
  145. }
  146. AVCodec ff_libspeex_decoder = {
  147. .name = "libspeex",
  148. .type = AVMEDIA_TYPE_AUDIO,
  149. .id = AV_CODEC_ID_SPEEX,
  150. .priv_data_size = sizeof(LibSpeexContext),
  151. .init = libspeex_decode_init,
  152. .close = libspeex_decode_close,
  153. .decode = libspeex_decode_frame,
  154. .flush = libspeex_decode_flush,
  155. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
  156. .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
  157. };