yading@10
|
1 /*
|
yading@10
|
2 * Copyright (C) 2009 Justin Ruggles
|
yading@10
|
3 * Copyright (c) 2009 Xuggle Incorporated
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * libspeex Speex audio encoder
|
yading@10
|
25 *
|
yading@10
|
26 * Usage Guide
|
yading@10
|
27 * This explains the values that need to be set prior to initialization in
|
yading@10
|
28 * order to control various encoding parameters.
|
yading@10
|
29 *
|
yading@10
|
30 * Channels
|
yading@10
|
31 * Speex only supports mono or stereo, so avctx->channels must be set to
|
yading@10
|
32 * 1 or 2.
|
yading@10
|
33 *
|
yading@10
|
34 * Sample Rate / Encoding Mode
|
yading@10
|
35 * Speex has 3 modes, each of which uses a specific sample rate.
|
yading@10
|
36 * narrowband : 8 kHz
|
yading@10
|
37 * wideband : 16 kHz
|
yading@10
|
38 * ultra-wideband : 32 kHz
|
yading@10
|
39 * avctx->sample_rate must be set to one of these 3 values. This will be
|
yading@10
|
40 * used to set the encoding mode.
|
yading@10
|
41 *
|
yading@10
|
42 * Rate Control
|
yading@10
|
43 * VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags.
|
yading@10
|
44 * avctx->global_quality is used to set the encoding quality.
|
yading@10
|
45 * For CBR mode, avctx->bit_rate can be used to set the constant bitrate.
|
yading@10
|
46 * Alternatively, the 'cbr_quality' option can be set from 0 to 10 to set
|
yading@10
|
47 * a constant bitrate based on quality.
|
yading@10
|
48 * For ABR mode, set avctx->bit_rate and set the 'abr' option to 1.
|
yading@10
|
49 * Approx. Bitrate Range:
|
yading@10
|
50 * narrowband : 2400 - 25600 bps
|
yading@10
|
51 * wideband : 4000 - 43200 bps
|
yading@10
|
52 * ultra-wideband : 4400 - 45200 bps
|
yading@10
|
53 *
|
yading@10
|
54 * Complexity
|
yading@10
|
55 * Encoding complexity is controlled by setting avctx->compression_level.
|
yading@10
|
56 * The valid range is 0 to 10. A higher setting gives generally better
|
yading@10
|
57 * quality at the expense of encoding speed. This does not affect the
|
yading@10
|
58 * bit rate.
|
yading@10
|
59 *
|
yading@10
|
60 * Frames-per-Packet
|
yading@10
|
61 * The encoder defaults to using 1 frame-per-packet. However, it is
|
yading@10
|
62 * sometimes desirable to use multiple frames-per-packet to reduce the
|
yading@10
|
63 * amount of container overhead. This can be done by setting the
|
yading@10
|
64 * 'frames_per_packet' option to a value 1 to 8.
|
yading@10
|
65 *
|
yading@10
|
66 *
|
yading@10
|
67 * Optional features
|
yading@10
|
68 * Speex encoder supports several optional features, which can be useful
|
yading@10
|
69 * for some conditions.
|
yading@10
|
70 *
|
yading@10
|
71 * Voice Activity Detection
|
yading@10
|
72 * When enabled, voice activity detection detects whether the audio
|
yading@10
|
73 * being encoded is speech or silence/background noise. VAD is always
|
yading@10
|
74 * implicitly activated when encoding in VBR, so the option is only useful
|
yading@10
|
75 * in non-VBR operation. In this case, Speex detects non-speech periods and
|
yading@10
|
76 * encodes them with just enough bits to reproduce the background noise.
|
yading@10
|
77 *
|
yading@10
|
78 * Discontinuous Transmission (DTX)
|
yading@10
|
79 * DTX is an addition to VAD/VBR operation, that allows to stop transmitting
|
yading@10
|
80 * completely when the background noise is stationary.
|
yading@10
|
81 * In file-based operation only 5 bits are used for such frames.
|
yading@10
|
82 */
|
yading@10
|
83
|
yading@10
|
84 #include <speex/speex.h>
|
yading@10
|
85 #include <speex/speex_header.h>
|
yading@10
|
86 #include <speex/speex_stereo.h>
|
yading@10
|
87
|
yading@10
|
88 #include "libavutil/channel_layout.h"
|
yading@10
|
89 #include "libavutil/common.h"
|
yading@10
|
90 #include "libavutil/opt.h"
|
yading@10
|
91 #include "avcodec.h"
|
yading@10
|
92 #include "internal.h"
|
yading@10
|
93 #include "audio_frame_queue.h"
|
yading@10
|
94
|
yading@10
|
95 /* TODO: Think about converting abr, vad, dtx and such flags to a bit field */
|
yading@10
|
96 typedef struct {
|
yading@10
|
97 AVClass *class; ///< AVClass for private options
|
yading@10
|
98 SpeexBits bits; ///< libspeex bitwriter context
|
yading@10
|
99 SpeexHeader header; ///< libspeex header struct
|
yading@10
|
100 void *enc_state; ///< libspeex encoder state
|
yading@10
|
101 int frames_per_packet; ///< number of frames to encode in each packet
|
yading@10
|
102 float vbr_quality; ///< VBR quality 0.0 to 10.0
|
yading@10
|
103 int cbr_quality; ///< CBR quality 0 to 10
|
yading@10
|
104 int abr; ///< flag to enable ABR
|
yading@10
|
105 int vad; ///< flag to enable VAD
|
yading@10
|
106 int dtx; ///< flag to enable DTX
|
yading@10
|
107 int pkt_frame_count; ///< frame count for the current packet
|
yading@10
|
108 AudioFrameQueue afq; ///< frame queue
|
yading@10
|
109 } LibSpeexEncContext;
|
yading@10
|
110
|
yading@10
|
111 static av_cold void print_enc_params(AVCodecContext *avctx,
|
yading@10
|
112 LibSpeexEncContext *s)
|
yading@10
|
113 {
|
yading@10
|
114 const char *mode_str = "unknown";
|
yading@10
|
115
|
yading@10
|
116 av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels);
|
yading@10
|
117 switch (s->header.mode) {
|
yading@10
|
118 case SPEEX_MODEID_NB: mode_str = "narrowband"; break;
|
yading@10
|
119 case SPEEX_MODEID_WB: mode_str = "wideband"; break;
|
yading@10
|
120 case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
|
yading@10
|
121 }
|
yading@10
|
122 av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str);
|
yading@10
|
123 if (s->header.vbr) {
|
yading@10
|
124 av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n");
|
yading@10
|
125 av_log(avctx, AV_LOG_DEBUG, " quality: %f\n", s->vbr_quality);
|
yading@10
|
126 } else if (s->abr) {
|
yading@10
|
127 av_log(avctx, AV_LOG_DEBUG, "rate control: ABR\n");
|
yading@10
|
128 av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate);
|
yading@10
|
129 } else {
|
yading@10
|
130 av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n");
|
yading@10
|
131 av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate);
|
yading@10
|
132 }
|
yading@10
|
133 av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n",
|
yading@10
|
134 avctx->compression_level);
|
yading@10
|
135 av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n",
|
yading@10
|
136 avctx->frame_size);
|
yading@10
|
137 av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n",
|
yading@10
|
138 s->frames_per_packet);
|
yading@10
|
139 av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n",
|
yading@10
|
140 avctx->frame_size * s->frames_per_packet);
|
yading@10
|
141 av_log(avctx, AV_LOG_DEBUG, "voice activity detection: %d\n", s->vad);
|
yading@10
|
142 av_log(avctx, AV_LOG_DEBUG, "discontinuous transmission: %d\n", s->dtx);
|
yading@10
|
143 }
|
yading@10
|
144
|
yading@10
|
145 static av_cold int encode_init(AVCodecContext *avctx)
|
yading@10
|
146 {
|
yading@10
|
147 LibSpeexEncContext *s = avctx->priv_data;
|
yading@10
|
148 const SpeexMode *mode;
|
yading@10
|
149 uint8_t *header_data;
|
yading@10
|
150 int header_size;
|
yading@10
|
151 int32_t complexity;
|
yading@10
|
152
|
yading@10
|
153 /* channels */
|
yading@10
|
154 if (avctx->channels < 1 || avctx->channels > 2) {
|
yading@10
|
155 av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
|
yading@10
|
156 "mono are supported\n", avctx->channels);
|
yading@10
|
157 return AVERROR(EINVAL);
|
yading@10
|
158 }
|
yading@10
|
159
|
yading@10
|
160 /* sample rate and encoding mode */
|
yading@10
|
161 switch (avctx->sample_rate) {
|
yading@10
|
162 case 8000: mode = &speex_nb_mode; break;
|
yading@10
|
163 case 16000: mode = &speex_wb_mode; break;
|
yading@10
|
164 case 32000: mode = &speex_uwb_mode; break;
|
yading@10
|
165 default:
|
yading@10
|
166 av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. "
|
yading@10
|
167 "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate);
|
yading@10
|
168 return AVERROR(EINVAL);
|
yading@10
|
169 }
|
yading@10
|
170
|
yading@10
|
171 /* initialize libspeex */
|
yading@10
|
172 s->enc_state = speex_encoder_init(mode);
|
yading@10
|
173 if (!s->enc_state) {
|
yading@10
|
174 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
|
yading@10
|
175 return -1;
|
yading@10
|
176 }
|
yading@10
|
177 speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
|
yading@10
|
178
|
yading@10
|
179 /* rate control method and parameters */
|
yading@10
|
180 if (avctx->flags & CODEC_FLAG_QSCALE) {
|
yading@10
|
181 /* VBR */
|
yading@10
|
182 s->header.vbr = 1;
|
yading@10
|
183 s->vad = 1; /* VAD is always implicitly activated for VBR */
|
yading@10
|
184 speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
|
yading@10
|
185 s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
|
yading@10
|
186 0.0f, 10.0f);
|
yading@10
|
187 speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality);
|
yading@10
|
188 } else {
|
yading@10
|
189 s->header.bitrate = avctx->bit_rate;
|
yading@10
|
190 if (avctx->bit_rate > 0) {
|
yading@10
|
191 /* CBR or ABR by bitrate */
|
yading@10
|
192 if (s->abr) {
|
yading@10
|
193 speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR,
|
yading@10
|
194 &s->header.bitrate);
|
yading@10
|
195 speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR,
|
yading@10
|
196 &s->header.bitrate);
|
yading@10
|
197 } else {
|
yading@10
|
198 speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE,
|
yading@10
|
199 &s->header.bitrate);
|
yading@10
|
200 speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
|
yading@10
|
201 &s->header.bitrate);
|
yading@10
|
202 }
|
yading@10
|
203 } else {
|
yading@10
|
204 /* CBR by quality */
|
yading@10
|
205 speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY,
|
yading@10
|
206 &s->cbr_quality);
|
yading@10
|
207 speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
|
yading@10
|
208 &s->header.bitrate);
|
yading@10
|
209 }
|
yading@10
|
210 /* stereo side information adds about 800 bps to the base bitrate */
|
yading@10
|
211 /* TODO: this should be calculated exactly */
|
yading@10
|
212 avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
|
yading@10
|
213 }
|
yading@10
|
214
|
yading@10
|
215 /* VAD is activated with VBR or can be turned on by itself */
|
yading@10
|
216 if (s->vad)
|
yading@10
|
217 speex_encoder_ctl(s->enc_state, SPEEX_SET_VAD, &s->vad);
|
yading@10
|
218
|
yading@10
|
219 /* Activiting Discontinuous Transmission */
|
yading@10
|
220 if (s->dtx) {
|
yading@10
|
221 speex_encoder_ctl(s->enc_state, SPEEX_SET_DTX, &s->dtx);
|
yading@10
|
222 if (!(s->abr || s->vad || s->header.vbr))
|
yading@10
|
223 av_log(avctx, AV_LOG_WARNING, "DTX is not much of use without ABR, VAD or VBR\n");
|
yading@10
|
224 }
|
yading@10
|
225
|
yading@10
|
226 /* set encoding complexity */
|
yading@10
|
227 if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
|
yading@10
|
228 complexity = av_clip(avctx->compression_level, 0, 10);
|
yading@10
|
229 speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
|
yading@10
|
230 }
|
yading@10
|
231 speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
|
yading@10
|
232 avctx->compression_level = complexity;
|
yading@10
|
233
|
yading@10
|
234 /* set packet size */
|
yading@10
|
235 avctx->frame_size = s->header.frame_size;
|
yading@10
|
236 s->header.frames_per_packet = s->frames_per_packet;
|
yading@10
|
237
|
yading@10
|
238 /* set encoding delay */
|
yading@10
|
239 speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->delay);
|
yading@10
|
240 ff_af_queue_init(avctx, &s->afq);
|
yading@10
|
241
|
yading@10
|
242 /* create header packet bytes from header struct */
|
yading@10
|
243 /* note: libspeex allocates the memory for header_data, which is freed
|
yading@10
|
244 below with speex_header_free() */
|
yading@10
|
245 header_data = speex_header_to_packet(&s->header, &header_size);
|
yading@10
|
246
|
yading@10
|
247 /* allocate extradata and coded_frame */
|
yading@10
|
248 avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
yading@10
|
249 if (!avctx->extradata) {
|
yading@10
|
250 speex_header_free(header_data);
|
yading@10
|
251 speex_encoder_destroy(s->enc_state);
|
yading@10
|
252 av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
|
yading@10
|
253 return AVERROR(ENOMEM);
|
yading@10
|
254 }
|
yading@10
|
255
|
yading@10
|
256 /* copy header packet to extradata */
|
yading@10
|
257 memcpy(avctx->extradata, header_data, header_size);
|
yading@10
|
258 avctx->extradata_size = header_size;
|
yading@10
|
259 speex_header_free(header_data);
|
yading@10
|
260
|
yading@10
|
261 /* init libspeex bitwriter */
|
yading@10
|
262 speex_bits_init(&s->bits);
|
yading@10
|
263
|
yading@10
|
264 print_enc_params(avctx, s);
|
yading@10
|
265 return 0;
|
yading@10
|
266 }
|
yading@10
|
267
|
yading@10
|
268 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
yading@10
|
269 const AVFrame *frame, int *got_packet_ptr)
|
yading@10
|
270 {
|
yading@10
|
271 LibSpeexEncContext *s = avctx->priv_data;
|
yading@10
|
272 int16_t *samples = frame ? (int16_t *)frame->data[0] : NULL;
|
yading@10
|
273 int ret;
|
yading@10
|
274
|
yading@10
|
275 if (samples) {
|
yading@10
|
276 /* encode Speex frame */
|
yading@10
|
277 if (avctx->channels == 2)
|
yading@10
|
278 speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
|
yading@10
|
279 speex_encode_int(s->enc_state, samples, &s->bits);
|
yading@10
|
280 s->pkt_frame_count++;
|
yading@10
|
281 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
|
yading@10
|
282 return ret;
|
yading@10
|
283 } else {
|
yading@10
|
284 /* handle end-of-stream */
|
yading@10
|
285 if (!s->pkt_frame_count)
|
yading@10
|
286 return 0;
|
yading@10
|
287 /* add extra terminator codes for unused frames in last packet */
|
yading@10
|
288 while (s->pkt_frame_count < s->frames_per_packet) {
|
yading@10
|
289 speex_bits_pack(&s->bits, 15, 5);
|
yading@10
|
290 s->pkt_frame_count++;
|
yading@10
|
291 }
|
yading@10
|
292 }
|
yading@10
|
293
|
yading@10
|
294 /* write output if all frames for the packet have been encoded */
|
yading@10
|
295 if (s->pkt_frame_count == s->frames_per_packet) {
|
yading@10
|
296 s->pkt_frame_count = 0;
|
yading@10
|
297 if ((ret = ff_alloc_packet2(avctx, avpkt, speex_bits_nbytes(&s->bits))) < 0)
|
yading@10
|
298 return ret;
|
yading@10
|
299 ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size);
|
yading@10
|
300 speex_bits_reset(&s->bits);
|
yading@10
|
301
|
yading@10
|
302 /* Get the next frame pts/duration */
|
yading@10
|
303 ff_af_queue_remove(&s->afq, s->frames_per_packet * avctx->frame_size,
|
yading@10
|
304 &avpkt->pts, &avpkt->duration);
|
yading@10
|
305
|
yading@10
|
306 avpkt->size = ret;
|
yading@10
|
307 *got_packet_ptr = 1;
|
yading@10
|
308 return 0;
|
yading@10
|
309 }
|
yading@10
|
310 return 0;
|
yading@10
|
311 }
|
yading@10
|
312
|
yading@10
|
313 static av_cold int encode_close(AVCodecContext *avctx)
|
yading@10
|
314 {
|
yading@10
|
315 LibSpeexEncContext *s = avctx->priv_data;
|
yading@10
|
316
|
yading@10
|
317 speex_bits_destroy(&s->bits);
|
yading@10
|
318 speex_encoder_destroy(s->enc_state);
|
yading@10
|
319
|
yading@10
|
320 ff_af_queue_close(&s->afq);
|
yading@10
|
321 av_freep(&avctx->extradata);
|
yading@10
|
322
|
yading@10
|
323 return 0;
|
yading@10
|
324 }
|
yading@10
|
325
|
yading@10
|
326 #define OFFSET(x) offsetof(LibSpeexEncContext, x)
|
yading@10
|
327 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
yading@10
|
328 static const AVOption options[] = {
|
yading@10
|
329 { "abr", "Use average bit rate", OFFSET(abr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
|
yading@10
|
330 { "cbr_quality", "Set quality value (0 to 10) for CBR", OFFSET(cbr_quality), AV_OPT_TYPE_INT, { .i64 = 8 }, 0, 10, AE },
|
yading@10
|
331 { "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 8, AE },
|
yading@10
|
332 { "vad", "Voice Activity Detection", OFFSET(vad), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
|
yading@10
|
333 { "dtx", "Discontinuous Transmission", OFFSET(dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
|
yading@10
|
334 { NULL },
|
yading@10
|
335 };
|
yading@10
|
336
|
yading@10
|
337 static const AVClass class = {
|
yading@10
|
338 .class_name = "libspeex",
|
yading@10
|
339 .item_name = av_default_item_name,
|
yading@10
|
340 .option = options,
|
yading@10
|
341 .version = LIBAVUTIL_VERSION_INT,
|
yading@10
|
342 };
|
yading@10
|
343
|
yading@10
|
344 static const AVCodecDefault defaults[] = {
|
yading@10
|
345 { "b", "0" },
|
yading@10
|
346 { "compression_level", "3" },
|
yading@10
|
347 { NULL },
|
yading@10
|
348 };
|
yading@10
|
349
|
yading@10
|
350 AVCodec ff_libspeex_encoder = {
|
yading@10
|
351 .name = "libspeex",
|
yading@10
|
352 .type = AVMEDIA_TYPE_AUDIO,
|
yading@10
|
353 .id = AV_CODEC_ID_SPEEX,
|
yading@10
|
354 .priv_data_size = sizeof(LibSpeexEncContext),
|
yading@10
|
355 .init = encode_init,
|
yading@10
|
356 .encode2 = encode_frame,
|
yading@10
|
357 .close = encode_close,
|
yading@10
|
358 .capabilities = CODEC_CAP_DELAY,
|
yading@10
|
359 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
|
yading@10
|
360 AV_SAMPLE_FMT_NONE },
|
yading@10
|
361 .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
|
yading@10
|
362 AV_CH_LAYOUT_STEREO,
|
yading@10
|
363 0 },
|
yading@10
|
364 .supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
|
yading@10
|
365 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
|
yading@10
|
366 .priv_class = &class,
|
yading@10
|
367 .defaults = defaults,
|
yading@10
|
368 };
|