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.

153 lines
4.6KB

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