1 /*
2 * Copyright (C) 2009 Justin Ruggles
3 * Copyright (c) 2009 Xuggle Incorporated
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * libspeex Speex audio encoder
25 *
26 * Usage Guide
27 * This explains the values that need to be set prior to initialization in
28 * order to control various encoding parameters.
29 *
30 * Channels
31 * Speex only supports mono or stereo, so avctx->channels must be set to
32 * 1 or 2.
33 *
34 * Sample Rate / Encoding Mode
35 * Speex has 3 modes, each of which uses a specific sample rate.
36 * narrowband : 8 kHz
37 * wideband : 16 kHz
38 * ultra-wideband : 32 kHz
39 * avctx->sample_rate must be set to one of these 3 values. This will be
40 * used to set the encoding mode.
41 *
42 * Rate Control
43 * VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags.
44 * avctx->global_quality is used to set the encoding quality.
45 * For CBR mode, avctx->bit_rate can be used to set the constant bitrate.
46 * Alternatively, the 'cbr_quality' option can be set from 0 to 10 to set
47 * a constant bitrate based on quality.
48 * For ABR mode, set avctx->bit_rate and set the 'abr' option to 1.
49 * Approx. Bitrate Range:
50 * narrowband : 2400 - 25600 bps
51 * wideband : 4000 - 43200 bps
52 * ultra-wideband : 4400 - 45200 bps
53 *
54 * Complexity
55 * Encoding complexity is controlled by setting avctx->compression_level.
56 * The valid range is 0 to 10. A higher setting gives generally better
57 * quality at the expense of encoding speed. This does not affect the
58 * bit rate.
59 *
60 * Frames-per-Packet
61 * The encoder defaults to using 1 frame-per-packet. However, it is
62 * sometimes desirable to use multiple frames-per-packet to reduce the
63 * amount of container overhead. This can be done by setting the
64 * 'frames_per_packet' option to a value 1 to 8.
65 *
66 *
67 * Optional features
68 * Speex encoder supports several optional features, which can be useful
69 * for some conditions.
70 *
71 * Voice Activity Detection
72 * When enabled, voice activity detection detects whether the audio
73 * being encoded is speech or silence/background noise. VAD is always
74 * implicitly activated when encoding in VBR, so the option is only useful
75 * in non-VBR operation. In this case, Speex detects non-speech periods and
76 * encodes them with just enough bits to reproduce the background noise.
77 *
78 * Discontinuous Transmission (DTX)
79 * DTX is an addition to VAD/VBR operation, that allows to stop transmitting
80 * completely when the background noise is stationary.
81 * In file-based operation only 5 bits are used for such frames.
82 */
83
84 #include <speex/speex.h>
85 #include <speex/speex_header.h>
86 #include <speex/speex_stereo.h>
87
94
95 /* TODO: Think about converting abr, vad, dtx and such flags to a bit field */
97 AVClass *
class;
///< AVClass for private options
98 SpeexBits
bits;
///< libspeex bitwriter context
99 SpeexHeader
header;
///< libspeex header struct
104 int abr;
///< flag to enable ABR
105 int vad;
///< flag to enable VAD
106 int dtx;
///< flag to enable DTX
110
113 {
114 const char *mode_str = "unknown";
115
118 case SPEEX_MODEID_NB: mode_str = "narrowband"; break;
119 case SPEEX_MODEID_WB: mode_str = "wideband"; break;
120 case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
121 }
129 } else {
132 }
143 }
144
146 {
148 const SpeexMode *
mode;
150 int header_size;
152
153 /* channels */
156 "mono are supported\n", avctx->
channels);
158 }
159
160 /* sample rate and encoding mode */
162 case 8000: mode = &speex_nb_mode; break;
163 case 16000: mode = &speex_wb_mode; break;
164 case 32000: mode = &speex_uwb_mode; break;
165 default:
167 "Resample to 8, 16, or 32 kHz.\n", avctx->
sample_rate);
169 }
170
171 /* initialize libspeex */
175 return -1;
176 }
178
179 /* rate control method and parameters */
181 /* VBR */
183 s->
vad = 1;
/* VAD is always implicitly activated for VBR */
186 0.0f, 10.0f);
188 } else {
191 /* CBR or ABR by bitrate */
193 speex_encoder_ctl(s->
enc_state, SPEEX_SET_ABR,
195 speex_encoder_ctl(s->
enc_state, SPEEX_GET_ABR,
197 } else {
198 speex_encoder_ctl(s->
enc_state, SPEEX_SET_BITRATE,
200 speex_encoder_ctl(s->
enc_state, SPEEX_GET_BITRATE,
202 }
203 } else {
204 /* CBR by quality */
205 speex_encoder_ctl(s->
enc_state, SPEEX_SET_QUALITY,
207 speex_encoder_ctl(s->
enc_state, SPEEX_GET_BITRATE,
209 }
210 /* stereo side information adds about 800 bps to the base bitrate */
211 /* TODO: this should be calculated exactly */
213 }
214
215 /* VAD is activated with VBR or can be turned on by itself */
217 speex_encoder_ctl(s->
enc_state, SPEEX_SET_VAD, &s->
vad);
218
219 /* Activiting Discontinuous Transmission */
221 speex_encoder_ctl(s->
enc_state, SPEEX_SET_DTX, &s->
dtx);
224 }
225
226 /* set encoding complexity */
229 speex_encoder_ctl(s->
enc_state, SPEEX_SET_COMPLEXITY, &complexity);
230 }
231 speex_encoder_ctl(s->
enc_state, SPEEX_GET_COMPLEXITY, &complexity);
233
234 /* set packet size */
237
238 /* set encoding delay */
239 speex_encoder_ctl(s->
enc_state, SPEEX_GET_LOOKAHEAD, &avctx->
delay);
241
242 /* create header packet bytes from header struct */
243 /* note: libspeex allocates the memory for header_data, which is freed
244 below with speex_header_free() */
245 header_data = speex_header_to_packet(&s->
header, &header_size);
246
247 /* allocate extradata and coded_frame */
250 speex_header_free(header_data);
254 }
255
256 /* copy header packet to extradata */
257 memcpy(avctx->
extradata, header_data, header_size);
259 speex_header_free(header_data);
260
261 /* init libspeex bitwriter */
262 speex_bits_init(&s->
bits);
263
265 return 0;
266 }
267
270 {
272 int16_t *samples = frame ? (int16_t *)frame->
data[0] : NULL;
274
276 /* encode Speex frame */
278 speex_encode_stereo_int(samples, s->
header.frame_size, &s->
bits);
283 } else {
284 /* handle end-of-stream */
286 return 0;
287 /* add extra terminator codes for unused frames in last packet */
289 speex_bits_pack(&s->
bits, 15, 5);
291 }
292 }
293
294 /* write output if all frames for the packet have been encoded */
300 speex_bits_reset(&s->
bits);
301
302 /* Get the next frame pts/duration */
305
307 *got_packet_ptr = 1;
308 return 0;
309 }
310 return 0;
311 }
312
314 {
316
317 speex_bits_destroy(&s->
bits);
319
322
323 return 0;
324 }
325
326 #define OFFSET(x) offsetof(LibSpeexEncContext, x)
327 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
330 {
"cbr_quality",
"Set quality value (0 to 10) for CBR",
OFFSET(cbr_quality),
AV_OPT_TYPE_INT, { .i64 = 8 }, 0, 10,
AE },
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 },
334 { NULL },
335 };
336
342 };
343
345 { "b", "0" },
346 { "compression_level", "3" },
347 { NULL },
348 };
349
364 0 },
365 .supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
368 };