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.

179 lines
6.1KB

  1. /*
  2. * Copyright (c) 2009 by Xuggle Incorporated. All rights reserved.
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <libavcodec/avcodec.h>
  20. #include <speex/speex.h>
  21. #include <speex/speex_header.h>
  22. #include <speex/speex_stereo.h>
  23. typedef struct {
  24. SpeexBits bits;
  25. void *enc_state;
  26. SpeexHeader header;
  27. } LibSpeexEncContext;
  28. static av_cold int libspeex_encode_init(AVCodecContext *avctx)
  29. {
  30. LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
  31. const SpeexMode *mode;
  32. if ((avctx->sample_fmt != SAMPLE_FMT_S16 && avctx->sample_fmt != SAMPLE_FMT_FLT) ||
  33. avctx->sample_rate <= 0 ||
  34. avctx->channels <= 0 ||
  35. avctx->channels > 2)
  36. {
  37. av_log(avctx, AV_LOG_ERROR, "Unsupported sample format, rate, or channels for speex");
  38. return -1;
  39. }
  40. if (avctx->sample_rate <= 8000)
  41. mode = &speex_nb_mode;
  42. else if (avctx->sample_rate <= 16000)
  43. mode = &speex_wb_mode;
  44. else
  45. mode = &speex_uwb_mode;
  46. speex_bits_init(&s->bits);
  47. s->enc_state = speex_encoder_init(mode);
  48. if (!s->enc_state)
  49. {
  50. av_log(avctx, AV_LOG_ERROR, "could not initialize speex encoder");
  51. return -1;
  52. }
  53. // initialize the header
  54. speex_init_header(&s->header, avctx->sample_rate,
  55. avctx->channels, mode);
  56. // TODO: It'd be nice to support VBR here, but
  57. // I'm uncertain what AVCodecContext options to use
  58. // to signal whether to turn it on.
  59. if (avctx->flags & CODEC_FLAG_QSCALE) {
  60. spx_int32_t quality = 0;
  61. // Map global_quality's mpeg 1/2/4 scale into Speex's 0-10 scale
  62. if (avctx->global_quality > FF_LAMBDA_MAX)
  63. quality = 0; // lowest possible quality
  64. else
  65. quality = (spx_int32_t)((FF_LAMBDA_MAX-avctx->global_quality)*10.0/FF_LAMBDA_MAX);
  66. speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY, &quality);
  67. } else {
  68. // default to CBR
  69. if (avctx->bit_rate > 0)
  70. speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, &avctx->bit_rate);
  71. // otherwise just take the default quality setting
  72. }
  73. // reset the bit-rate to the actual bit rate speex will use
  74. speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, &s->header.bitrate);
  75. avctx->bit_rate = s->header.bitrate;
  76. // get the actual sample rate
  77. speex_encoder_ctl(s->enc_state, SPEEX_GET_SAMPLING_RATE, &s->header.rate);
  78. avctx->sample_rate = s->header.rate;
  79. // get the frame-size. To align with FLV, we're going to put 2 frames
  80. // per packet. If someone can tell me how to make this configurable
  81. // from the avcodec contents, I'll mod this so it's not hard-coded.
  82. // but without this, FLV files with speex data won't play correctly
  83. // in flash player 10.
  84. speex_encoder_ctl(s->enc_state, SPEEX_GET_FRAME_SIZE, &s->header.frame_size);
  85. s->header.frames_per_packet = 2; // Need for FLV container support
  86. avctx->frame_size = s->header.frame_size*s->header.frames_per_packet;
  87. // and we'll put a speex header packet into extradata so that muxers
  88. // can use it.
  89. avctx->extradata = speex_header_to_packet(&s->header, &avctx->extradata_size);
  90. return 0;
  91. }
  92. static av_cold int libspeex_encode_frame(
  93. AVCodecContext *avctx, uint8_t *frame,
  94. int buf_size, void *data)
  95. {
  96. LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
  97. int i = 0;
  98. if (!data)
  99. // nothing to flush
  100. return 0;
  101. speex_bits_reset(&s->bits);
  102. for(i = 0; i < s->header.frames_per_packet; i++)
  103. {
  104. if (avctx->sample_fmt == SAMPLE_FMT_FLT)
  105. {
  106. if (avctx->channels == 2) {
  107. speex_encode_stereo(
  108. (float*)data+i*s->header.frame_size,
  109. s->header.frame_size,
  110. &s->bits);
  111. }
  112. speex_encode(s->enc_state,
  113. (float*)data+i*s->header.frame_size, &s->bits);
  114. } else {
  115. if (avctx->channels == 2) {
  116. speex_encode_stereo_int(
  117. (spx_int16_t*)data+i*s->header.frame_size,
  118. s->header.frame_size,
  119. &s->bits);
  120. }
  121. speex_encode_int(s->enc_state,
  122. (spx_int16_t*)data+i*s->header.frame_size, &s->bits);
  123. }
  124. }
  125. // put in a terminator so this will fit in a OGG or FLV packet
  126. speex_bits_insert_terminator(&s->bits);
  127. if (buf_size >= speex_bits_nbytes(&s->bits)) {
  128. return speex_bits_write(&s->bits, frame, buf_size);
  129. } else {
  130. av_log(avctx, AV_LOG_ERROR, "output buffer too small");
  131. return -1;
  132. }
  133. }
  134. static av_cold int libspeex_encode_close(AVCodecContext *avctx)
  135. {
  136. LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
  137. speex_bits_destroy(&s->bits);
  138. speex_encoder_destroy(s->enc_state);
  139. s->enc_state = 0;
  140. if (avctx->extradata)
  141. speex_header_free(avctx->extradata);
  142. avctx->extradata = 0;
  143. avctx->extradata_size = 0;
  144. return 0;
  145. }
  146. AVCodec ff_libspeex_encoder = {
  147. "libspeex",
  148. AVMEDIA_TYPE_AUDIO,
  149. CODEC_ID_SPEEX,
  150. sizeof(LibSpeexEncContext),
  151. libspeex_encode_init,
  152. libspeex_encode_frame,
  153. libspeex_encode_close,
  154. 0,
  155. .capabilities = CODEC_CAP_DELAY,
  156. .supported_samplerates = (const int[]){8000, 16000, 32000, 0},
  157. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_FLT,SAMPLE_FMT_NONE},
  158. .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex Encoder"),
  159. };