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.

187 lines
6.0KB

  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/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. typedef struct {
  29. AVFrame frame;
  30. SpeexBits bits;
  31. SpeexStereoState stereo;
  32. void *dec_state;
  33. int frame_size;
  34. } LibSpeexContext;
  35. static av_cold int libspeex_decode_init(AVCodecContext *avctx)
  36. {
  37. LibSpeexContext *s = avctx->priv_data;
  38. const SpeexMode *mode;
  39. SpeexHeader *header = NULL;
  40. int spx_mode;
  41. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  42. if (avctx->extradata && avctx->extradata_size >= 80) {
  43. header = speex_packet_to_header(avctx->extradata,
  44. avctx->extradata_size);
  45. if (!header)
  46. av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
  47. }
  48. if (header) {
  49. avctx->channels = header->nb_channels;
  50. spx_mode = header->mode;
  51. speex_header_free(header);
  52. } else {
  53. switch (avctx->sample_rate) {
  54. case 8000: spx_mode = 0; break;
  55. case 16000: spx_mode = 1; break;
  56. case 32000: spx_mode = 2; break;
  57. default:
  58. /* libspeex can handle any mode if initialized as ultra-wideband */
  59. av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
  60. "Decoding as 32kHz ultra-wideband\n",
  61. avctx->sample_rate);
  62. spx_mode = 2;
  63. }
  64. }
  65. mode = speex_lib_get_mode(spx_mode);
  66. if (!mode) {
  67. av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
  68. return AVERROR_INVALIDDATA;
  69. }
  70. avctx->sample_rate = 8000 << spx_mode;
  71. s->frame_size = 160 << spx_mode;
  72. if (avctx->channels < 1 || avctx->channels > 2) {
  73. /* libspeex can handle mono or stereo if initialized as stereo */
  74. av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
  75. "Decoding as stereo.\n", avctx->channels);
  76. avctx->channels = 2;
  77. }
  78. avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
  79. AV_CH_LAYOUT_MONO;
  80. speex_bits_init(&s->bits);
  81. s->dec_state = speex_decoder_init(mode);
  82. if (!s->dec_state) {
  83. av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
  84. return -1;
  85. }
  86. if (avctx->channels == 2) {
  87. SpeexCallback callback;
  88. callback.callback_id = SPEEX_INBAND_STEREO;
  89. callback.func = speex_std_stereo_request_handler;
  90. callback.data = &s->stereo;
  91. s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
  92. speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
  93. }
  94. avcodec_get_frame_defaults(&s->frame);
  95. avctx->coded_frame = &s->frame;
  96. return 0;
  97. }
  98. static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
  99. int *got_frame_ptr, AVPacket *avpkt)
  100. {
  101. uint8_t *buf = avpkt->data;
  102. int buf_size = avpkt->size;
  103. LibSpeexContext *s = avctx->priv_data;
  104. int16_t *output;
  105. int ret, consumed = 0;
  106. /* get output buffer */
  107. s->frame.nb_samples = s->frame_size;
  108. if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
  109. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  110. return ret;
  111. }
  112. output = (int16_t *)s->frame.data[0];
  113. /* if there is not enough data left for the smallest possible frame or the
  114. next 5 bits are a terminator code, reset the libspeex buffer using the
  115. current packet, otherwise ignore the current packet and keep decoding
  116. frames from the libspeex buffer. */
  117. if (speex_bits_remaining(&s->bits) < 5 ||
  118. speex_bits_peek_unsigned(&s->bits, 5) == 0x1F) {
  119. /* check for flush packet */
  120. if (!buf || !buf_size) {
  121. *got_frame_ptr = 0;
  122. return buf_size;
  123. }
  124. /* set new buffer */
  125. speex_bits_read_from(&s->bits, buf, buf_size);
  126. consumed = buf_size;
  127. }
  128. /* decode a single frame */
  129. ret = speex_decode_int(s->dec_state, &s->bits, output);
  130. if (ret <= -2) {
  131. av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
  132. return AVERROR_INVALIDDATA;
  133. }
  134. if (avctx->channels == 2)
  135. speex_decode_stereo_int(output, s->frame_size, &s->stereo);
  136. *got_frame_ptr = 1;
  137. *(AVFrame *)data = s->frame;
  138. return consumed;
  139. }
  140. static av_cold int libspeex_decode_close(AVCodecContext *avctx)
  141. {
  142. LibSpeexContext *s = avctx->priv_data;
  143. speex_bits_destroy(&s->bits);
  144. speex_decoder_destroy(s->dec_state);
  145. return 0;
  146. }
  147. static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
  148. {
  149. LibSpeexContext *s = avctx->priv_data;
  150. speex_bits_reset(&s->bits);
  151. }
  152. AVCodec ff_libspeex_decoder = {
  153. .name = "libspeex",
  154. .type = AVMEDIA_TYPE_AUDIO,
  155. .id = AV_CODEC_ID_SPEEX,
  156. .priv_data_size = sizeof(LibSpeexContext),
  157. .init = libspeex_decode_init,
  158. .close = libspeex_decode_close,
  159. .decode = libspeex_decode_frame,
  160. .flush = libspeex_decode_flush,
  161. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
  162. .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
  163. };