2 * Copyright (c) 2009 by Xuggle Incorporated. All rights reserved.
3 * This file is part of FFmpeg.
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.
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.
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
19 #include <libavcodec/avcodec.h>
20 #include <speex/speex.h>
21 #include <speex/speex_header.h>
22 #include <speex/speex_stereo.h>
31 static av_cold int libspeex_encode_init(AVCodecContext *avctx)
33 LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
34 const SpeexMode *mode;
36 if ((avctx->sample_fmt != SAMPLE_FMT_S16 && avctx->sample_fmt != SAMPLE_FMT_FLT) ||
37 avctx->sample_rate <= 0 ||
38 avctx->channels <= 0 ||
41 av_log(avctx, AV_LOG_ERROR, "Unsupported sample format, rate, or channels for speex");
45 if (avctx->sample_rate <= 8000)
46 mode = &speex_nb_mode;
47 else if (avctx->sample_rate <= 16000)
48 mode = &speex_wb_mode;
50 mode = &speex_uwb_mode;
52 speex_bits_init(&s->bits);
53 s->enc_state = speex_encoder_init(mode);
56 av_log(avctx, AV_LOG_ERROR, "could not initialize speex encoder");
60 // initialize the header
61 speex_init_header(&s->header, avctx->sample_rate,
62 avctx->channels, mode);
64 // TODO: It'd be nice to support VBR here, but
65 // I'm uncertain what AVCodecContext options to use
66 // to signal whether to turn it on.
67 if (avctx->flags & CODEC_FLAG_QSCALE) {
68 spx_int32_t quality = 0;
69 // Map global_quality's mpeg 1/2/4 scale into Speex's 0-10 scale
70 if (avctx->global_quality > FF_LAMBDA_MAX)
71 quality = 0; // lowest possible quality
73 quality = (spx_int32_t)((FF_LAMBDA_MAX-avctx->global_quality)*10.0/FF_LAMBDA_MAX);
74 speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY, &quality);
77 if (avctx->bit_rate > 0)
78 speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, &avctx->bit_rate);
79 // otherwise just take the default quality setting
81 // reset the bit-rate to the actual bit rate speex will use
82 speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, &s->header.bitrate);
83 avctx->bit_rate = s->header.bitrate;
85 // get the actual sample rate
86 speex_encoder_ctl(s->enc_state, SPEEX_GET_SAMPLING_RATE, &s->header.rate);
87 avctx->sample_rate = s->header.rate;
89 // get the frame-size. To align with FLV, we're going to put 2 frames
90 // per packet. If someone can tell me how to make this configurable
91 // from the avcodec contents, I'll mod this so it's not hard-coded.
92 // but without this, FLV files with speex data won't play correctly
93 // in flash player 10.
94 speex_encoder_ctl(s->enc_state, SPEEX_GET_FRAME_SIZE, &s->header.frame_size);
95 s->header.frames_per_packet = 2; // Need for FLV container support
96 avctx->frame_size = s->header.frame_size*s->header.frames_per_packet;
98 // and we'll put a speex header packet into extradata so that muxers
100 avctx->extradata = speex_header_to_packet(&s->header, &avctx->extradata_size);
104 static av_cold int libspeex_encode_frame(
105 AVCodecContext *avctx, uint8_t *frame,
106 int buf_size, void *data)
108 LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
115 speex_bits_reset(&s->bits);
116 for(i = 0; i < s->header.frames_per_packet; i++)
118 if (avctx->sample_fmt == SAMPLE_FMT_FLT)
120 if (avctx->channels == 2) {
122 (float*)data+i*s->header.frame_size,
123 s->header.frame_size,
126 speex_encode(s->enc_state,
127 (float*)data+i*s->header.frame_size, &s->bits);
129 if (avctx->channels == 2) {
130 speex_encode_stereo_int(
131 (spx_int16_t*)data+i*s->header.frame_size,
132 s->header.frame_size,
135 speex_encode_int(s->enc_state,
136 (spx_int16_t*)data+i*s->header.frame_size, &s->bits);
139 // put in a terminator so this will fit in a OGG or FLV packet
140 speex_bits_insert_terminator(&s->bits);
142 if (buf_size >= speex_bits_nbytes(&s->bits)) {
143 return speex_bits_write(&s->bits, frame, buf_size);
145 av_log(avctx, AV_LOG_ERROR, "output buffer too small");
150 static av_cold int libspeex_encode_close(AVCodecContext *avctx)
152 LibSpeexEncContext *s = (LibSpeexEncContext*)avctx->priv_data;
154 speex_bits_destroy(&s->bits);
155 speex_encoder_destroy(s->enc_state);
157 if (avctx->extradata)
158 speex_header_free(avctx->extradata);
159 avctx->extradata = 0;
160 avctx->extradata_size = 0;
165 AVCodec ff_libspeex_encoder = {
169 sizeof(LibSpeexEncContext),
170 libspeex_encode_init,
171 libspeex_encode_frame,
172 libspeex_encode_close,
174 .capabilities = CODEC_CAP_DELAY,
175 .supported_samplerates = (const int[]){8000, 16000, 32000, 0},
176 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_FLT,SAMPLE_FMT_NONE},
177 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex Encoder"),