libspeexdec.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "avcodec.h"
  21. #include <speex/speex.h>
  22. #include <speex/speex_header.h>
  23. #include <speex/speex_stereo.h>
  24. #include <speex/speex_callbacks.h>
  25. typedef struct {
  26. SpeexBits bits;
  27. SpeexStereoState stereo;
  28. void *dec_state;
  29. SpeexHeader *header;
  30. } LibSpeexContext;
  31. static av_cold int libspeex_decode_init(AVCodecContext *avctx)
  32. {
  33. LibSpeexContext *s = avctx->priv_data;
  34. const SpeexMode *mode;
  35. // defaults in the case of a missing header
  36. if (avctx->sample_rate <= 8000)
  37. mode = &speex_nb_mode;
  38. else if (avctx->sample_rate <= 16000)
  39. mode = &speex_wb_mode;
  40. else
  41. mode = &speex_uwb_mode;
  42. if (avctx->extradata_size >= 80)
  43. s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
  44. avctx->sample_fmt = SAMPLE_FMT_S16;
  45. if (s->header) {
  46. avctx->sample_rate = s->header->rate;
  47. avctx->channels = s->header->nb_channels;
  48. avctx->frame_size = s->header->frame_size;
  49. mode = speex_lib_get_mode(s->header->mode);
  50. if (!mode) {
  51. av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode);
  52. return -1;
  53. }
  54. } else
  55. av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n");
  56. if (avctx->channels > 2) {
  57. av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n");
  58. return -1;
  59. }
  60. speex_bits_init(&s->bits);
  61. s->dec_state = speex_decoder_init(mode);
  62. if (!s->dec_state) {
  63. av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
  64. return -1;
  65. }
  66. if (!s->header)
  67. speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &avctx->frame_size);
  68. if (avctx->channels == 2) {
  69. SpeexCallback callback;
  70. callback.callback_id = SPEEX_INBAND_STEREO;
  71. callback.func = speex_std_stereo_request_handler;
  72. callback.data = &s->stereo;
  73. s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
  74. speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
  75. }
  76. return 0;
  77. }
  78. static int libspeex_decode_frame(AVCodecContext *avctx,
  79. void *data, int *data_size,
  80. const uint8_t *buf, int buf_size)
  81. {
  82. LibSpeexContext *s = avctx->priv_data;
  83. int16_t *output = data, *end;
  84. int i, num_samples;
  85. num_samples = avctx->frame_size * avctx->channels;
  86. end = output + *data_size/2;
  87. speex_bits_read_from(&s->bits, buf, buf_size);
  88. for (i = 0; speex_bits_remaining(&s->bits) && output + num_samples < end; i++) {
  89. int ret = speex_decode_int(s->dec_state, &s->bits, output);
  90. if (ret <= -2) {
  91. av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
  92. return -1;
  93. } else if (ret == -1)
  94. // end of stream
  95. break;
  96. if (avctx->channels == 2)
  97. speex_decode_stereo_int(output, avctx->frame_size, &s->stereo);
  98. output += num_samples;
  99. }
  100. *data_size = i * avctx->channels * avctx->frame_size * 2;
  101. return buf_size;
  102. }
  103. static av_cold int libspeex_decode_close(AVCodecContext *avctx)
  104. {
  105. LibSpeexContext *s = avctx->priv_data;
  106. speex_header_free(s->header);
  107. speex_bits_destroy(&s->bits);
  108. speex_decoder_destroy(s->dec_state);
  109. return 0;
  110. }
  111. AVCodec libspeex_decoder = {
  112. "libspeex",
  113. CODEC_TYPE_AUDIO,
  114. CODEC_ID_SPEEX,
  115. sizeof(LibSpeexContext),
  116. libspeex_decode_init,
  117. NULL,
  118. libspeex_decode_close,
  119. libspeex_decode_frame,
  120. .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
  121. };