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.

205 lines
6.8KB

  1. /*
  2. * Copyright (C) 2008 David Conrad
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 LibSpeexContext {
  29. SpeexBits bits;
  30. SpeexStereoState stereo;
  31. void *dec_state;
  32. int frame_size;
  33. int pktsize;
  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. if (avctx->extradata && avctx->extradata_size >= 80) {
  42. header = speex_packet_to_header(avctx->extradata,
  43. avctx->extradata_size);
  44. if (!header)
  45. av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
  46. }
  47. if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
  48. int quality;
  49. if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
  50. av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
  51. return AVERROR_INVALIDDATA;
  52. }
  53. quality = avctx->extradata[37];
  54. if (quality > 10) {
  55. av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode %d.\n", quality);
  56. return AVERROR_PATCHWELCOME;
  57. }
  58. s->pktsize = ((const int[]){5,10,15,20,20,28,28,38,38,46,62})[quality];
  59. spx_mode = 0;
  60. } else if (header) {
  61. avctx->sample_rate = header->rate;
  62. avctx->channels = header->nb_channels;
  63. spx_mode = header->mode;
  64. speex_header_free(header);
  65. } else {
  66. switch (avctx->sample_rate) {
  67. case 8000: spx_mode = 0; break;
  68. case 16000: spx_mode = 1; break;
  69. case 32000: spx_mode = 2; break;
  70. default:
  71. /* libspeex can handle any mode if initialized as ultra-wideband */
  72. av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
  73. "Decoding as 32kHz ultra-wideband\n",
  74. avctx->sample_rate);
  75. spx_mode = 2;
  76. }
  77. }
  78. mode = speex_lib_get_mode(spx_mode);
  79. if (!mode) {
  80. av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
  81. return AVERROR_INVALIDDATA;
  82. }
  83. s->frame_size = 160 << spx_mode;
  84. if (!avctx->sample_rate)
  85. avctx->sample_rate = 8000 << spx_mode;
  86. if (avctx->channels < 1 || avctx->channels > 2) {
  87. /* libspeex can handle mono or stereo if initialized as stereo */
  88. av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
  89. "Decoding as stereo.\n", avctx->channels);
  90. avctx->channels = 2;
  91. }
  92. avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
  93. AV_CH_LAYOUT_MONO;
  94. speex_bits_init(&s->bits);
  95. s->dec_state = speex_decoder_init(mode);
  96. if (!s->dec_state) {
  97. av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
  98. return -1;
  99. }
  100. if (avctx->channels == 2) {
  101. SpeexCallback callback;
  102. callback.callback_id = SPEEX_INBAND_STEREO;
  103. callback.func = speex_std_stereo_request_handler;
  104. callback.data = &s->stereo;
  105. s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
  106. speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
  107. }
  108. return 0;
  109. }
  110. static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
  111. int *got_frame_ptr, AVPacket *avpkt)
  112. {
  113. uint8_t *buf = avpkt->data;
  114. int buf_size = avpkt->size;
  115. LibSpeexContext *s = avctx->priv_data;
  116. AVFrame *frame = data;
  117. int16_t *output;
  118. int ret, consumed = 0;
  119. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  120. /* get output buffer */
  121. frame->nb_samples = s->frame_size;
  122. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  123. return ret;
  124. output = (int16_t *)frame->data[0];
  125. /* if there is not enough data left for the smallest possible frame or the
  126. next 5 bits are a terminator code, reset the libspeex buffer using the
  127. current packet, otherwise ignore the current packet and keep decoding
  128. frames from the libspeex buffer. */
  129. if (speex_bits_remaining(&s->bits) < 5 ||
  130. speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
  131. /* check for flush packet */
  132. if (!buf || !buf_size) {
  133. *got_frame_ptr = 0;
  134. return buf_size;
  135. }
  136. if (s->pktsize && buf_size == 62)
  137. buf_size = s->pktsize;
  138. /* set new buffer */
  139. speex_bits_read_from(&s->bits, buf, buf_size);
  140. consumed = avpkt->size;
  141. }
  142. /* decode a single frame */
  143. ret = speex_decode_int(s->dec_state, &s->bits, output);
  144. if (ret <= -2) {
  145. av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. if (avctx->channels == 2)
  149. speex_decode_stereo_int(output, s->frame_size, &s->stereo);
  150. *got_frame_ptr = 1;
  151. if (!avctx->bit_rate)
  152. speex_decoder_ctl(s->dec_state, SPEEX_GET_BITRATE, &avctx->bit_rate);
  153. return consumed;
  154. }
  155. static av_cold int libspeex_decode_close(AVCodecContext *avctx)
  156. {
  157. LibSpeexContext *s = avctx->priv_data;
  158. speex_bits_destroy(&s->bits);
  159. speex_decoder_destroy(s->dec_state);
  160. return 0;
  161. }
  162. static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
  163. {
  164. LibSpeexContext *s = avctx->priv_data;
  165. speex_bits_reset(&s->bits);
  166. }
  167. AVCodec ff_libspeex_decoder = {
  168. .name = "libspeex",
  169. .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
  170. .type = AVMEDIA_TYPE_AUDIO,
  171. .id = AV_CODEC_ID_SPEEX,
  172. .priv_data_size = sizeof(LibSpeexContext),
  173. .init = libspeex_decode_init,
  174. .close = libspeex_decode_close,
  175. .decode = libspeex_decode_frame,
  176. .flush = libspeex_decode_flush,
  177. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  178. .wrapper_name = "libspeex",
  179. };