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->ch_layout.nb_channels must
32 * be set to 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 AV_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 makes it possible 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
88 #include "libavutil/channel_layout.h"
89 #include "libavutil/common.h"
90 #include "libavutil/opt.h"
91 #include "avcodec.h"
92 #include "codec_internal.h"
93 #include "encode.h"
94 #include "audio_frame_queue.h"
95
96 /* TODO: Think about converting abr, vad, dtx and such flags to a bit field */
97 typedef struct LibSpeexEncContext {
98 AVClass *class; ///< AVClass for private options
99 SpeexBits bits; ///< libspeex bitwriter context
100 SpeexHeader header; ///< libspeex header struct
101 void *enc_state; ///< libspeex encoder state
102 int frames_per_packet; ///< number of frames to encode in each packet
103 float vbr_quality; ///< VBR quality 0.0 to 10.0
104 int cbr_quality; ///< CBR quality 0 to 10
105 int abr; ///< flag to enable ABR
106 int vad; ///< flag to enable VAD
107 int dtx; ///< flag to enable DTX
108 int pkt_frame_count; ///< frame count for the current packet
109 AudioFrameQueue afq; ///< frame queue
110 } LibSpeexEncContext;
111
print_enc_params(AVCodecContext *avctx, LibSpeexEncContext *s)112 static av_cold void print_enc_params(AVCodecContext *avctx,
113 LibSpeexEncContext *s)
114 {
115 const char *mode_str = "unknown";
116
117 av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->ch_layout.nb_channels);
118 switch (s->header.mode) {
119 case SPEEX_MODEID_NB: mode_str = "narrowband"; break;
120 case SPEEX_MODEID_WB: mode_str = "wideband"; break;
121 case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break;
122 }
123 av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str);
124 if (s->header.vbr) {
125 av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n");
126 av_log(avctx, AV_LOG_DEBUG, " quality: %f\n", s->vbr_quality);
127 } else if (s->abr) {
128 av_log(avctx, AV_LOG_DEBUG, "rate control: ABR\n");
129 av_log(avctx, AV_LOG_DEBUG, " bitrate: %"PRId64" bps\n", avctx->bit_rate);
130 } else {
131 av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n");
132 av_log(avctx, AV_LOG_DEBUG, " bitrate: %"PRId64" bps\n", avctx->bit_rate);
133 }
134 av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n",
135 avctx->compression_level);
136 av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n",
137 avctx->frame_size);
138 av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n",
139 s->frames_per_packet);
140 av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n",
141 avctx->frame_size * s->frames_per_packet);
142 av_log(avctx, AV_LOG_DEBUG, "voice activity detection: %d\n", s->vad);
143 av_log(avctx, AV_LOG_DEBUG, "discontinuous transmission: %d\n", s->dtx);
144 }
145
encode_init(AVCodecContext *avctx)146 static av_cold int encode_init(AVCodecContext *avctx)
147 {
148 LibSpeexEncContext *s = avctx->priv_data;
149 int channels = avctx->ch_layout.nb_channels;
150 const SpeexMode *mode;
151 uint8_t *header_data;
152 int header_size;
153 int32_t complexity;
154
155 /* channels */
156 if (channels < 1 || channels > 2) {
157 av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and "
158 "mono are supported\n", channels);
159 return AVERROR(EINVAL);
160 }
161
162 /* sample rate and encoding mode */
163 switch (avctx->sample_rate) {
164 case 8000: mode = speex_lib_get_mode(SPEEX_MODEID_NB); break;
165 case 16000: mode = speex_lib_get_mode(SPEEX_MODEID_WB); break;
166 case 32000: mode = speex_lib_get_mode(SPEEX_MODEID_UWB); break;
167 default:
168 av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. "
169 "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate);
170 return AVERROR(EINVAL);
171 }
172
173 /* initialize libspeex */
174 s->enc_state = speex_encoder_init(mode);
175 if (!s->enc_state) {
176 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n");
177 return -1;
178 }
179 speex_init_header(&s->header, avctx->sample_rate, channels, mode);
180
181 /* rate control method and parameters */
182 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
183 /* VBR */
184 s->header.vbr = 1;
185 s->vad = 1; /* VAD is always implicitly activated for VBR */
186 speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
187 s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
188 0.0f, 10.0f);
189 speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality);
190 } else {
191 s->header.bitrate = avctx->bit_rate;
192 if (avctx->bit_rate > 0) {
193 /* CBR or ABR by bitrate */
194 if (s->abr) {
195 speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR,
196 &s->header.bitrate);
197 speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR,
198 &s->header.bitrate);
199 } else {
200 speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE,
201 &s->header.bitrate);
202 speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
203 &s->header.bitrate);
204 }
205 } else {
206 /* CBR by quality */
207 speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY,
208 &s->cbr_quality);
209 speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE,
210 &s->header.bitrate);
211 }
212 /* stereo side information adds about 800 bps to the base bitrate */
213 /* TODO: this should be calculated exactly */
214 avctx->bit_rate = s->header.bitrate + (channels == 2 ? 800 : 0);
215 }
216
217 /* VAD is activated with VBR or can be turned on by itself */
218 if (s->vad)
219 speex_encoder_ctl(s->enc_state, SPEEX_SET_VAD, &s->vad);
220
221 /* Activating Discontinuous Transmission */
222 if (s->dtx) {
223 speex_encoder_ctl(s->enc_state, SPEEX_SET_DTX, &s->dtx);
224 if (!(s->abr || s->vad || s->header.vbr))
225 av_log(avctx, AV_LOG_WARNING, "DTX is not much of use without ABR, VAD or VBR\n");
226 }
227
228 /* set encoding complexity */
229 if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
230 complexity = av_clip(avctx->compression_level, 0, 10);
231 speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
232 }
233 speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
234 avctx->compression_level = complexity;
235
236 /* set packet size */
237 avctx->frame_size = s->header.frame_size;
238 s->header.frames_per_packet = s->frames_per_packet;
239
240 /* set encoding delay */
241 speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->initial_padding);
242 ff_af_queue_init(avctx, &s->afq);
243
244 /* create header packet bytes from header struct */
245 /* note: libspeex allocates the memory for header_data, which is freed
246 below with speex_header_free() */
247 header_data = speex_header_to_packet(&s->header, &header_size);
248
249 /* allocate extradata */
250 avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
251 if (!avctx->extradata) {
252 speex_header_free(header_data);
253 speex_encoder_destroy(s->enc_state);
254 av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
255 return AVERROR(ENOMEM);
256 }
257
258 /* copy header packet to extradata */
259 memcpy(avctx->extradata, header_data, header_size);
260 avctx->extradata_size = header_size;
261 speex_header_free(header_data);
262
263 /* init libspeex bitwriter */
264 speex_bits_init(&s->bits);
265
266 print_enc_params(avctx, s);
267 return 0;
268 }
269
encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)270 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
271 const AVFrame *frame, int *got_packet_ptr)
272 {
273 LibSpeexEncContext *s = avctx->priv_data;
274 int16_t *samples = frame ? (int16_t *)frame->data[0] : NULL;
275 int ret;
276
277 if (samples) {
278 /* encode Speex frame */
279 if (avctx->ch_layout.nb_channels == 2)
280 speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
281 speex_encode_int(s->enc_state, samples, &s->bits);
282 s->pkt_frame_count++;
283 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
284 return ret;
285 } else {
286 /* handle end-of-stream */
287 if (!s->pkt_frame_count)
288 return 0;
289 /* add extra terminator codes for unused frames in last packet */
290 while (s->pkt_frame_count < s->frames_per_packet) {
291 speex_bits_pack(&s->bits, 15, 5);
292 s->pkt_frame_count++;
293 }
294 }
295
296 /* write output if all frames for the packet have been encoded */
297 if (s->pkt_frame_count == s->frames_per_packet) {
298 s->pkt_frame_count = 0;
299 if ((ret = ff_alloc_packet(avctx, avpkt, speex_bits_nbytes(&s->bits))) < 0)
300 return ret;
301 ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size);
302 speex_bits_reset(&s->bits);
303
304 /* Get the next frame pts/duration */
305 ff_af_queue_remove(&s->afq, s->frames_per_packet * avctx->frame_size,
306 &avpkt->pts, &avpkt->duration);
307
308 avpkt->size = ret;
309 *got_packet_ptr = 1;
310 return 0;
311 }
312 return 0;
313 }
314
encode_close(AVCodecContext *avctx)315 static av_cold int encode_close(AVCodecContext *avctx)
316 {
317 LibSpeexEncContext *s = avctx->priv_data;
318
319 speex_bits_destroy(&s->bits);
320 speex_encoder_destroy(s->enc_state);
321
322 ff_af_queue_close(&s->afq);
323
324 return 0;
325 }
326
327 #define OFFSET(x) offsetof(LibSpeexEncContext, x)
328 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
329 static const AVOption options[] = {
330 { "abr", "Use average bit rate", OFFSET(abr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
331 { "cbr_quality", "Set quality value (0 to 10) for CBR", OFFSET(cbr_quality), AV_OPT_TYPE_INT, { .i64 = 8 }, 0, 10, AE },
332 { "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 8, AE },
333 { "vad", "Voice Activity Detection", OFFSET(vad), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
334 { "dtx", "Discontinuous Transmission", OFFSET(dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
335 { NULL },
336 };
337
338 static const AVClass speex_class = {
339 .class_name = "libspeex",
340 .item_name = av_default_item_name,
341 .option = options,
342 .version = LIBAVUTIL_VERSION_INT,
343 };
344
345 static const FFCodecDefault defaults[] = {
346 { "b", "0" },
347 { "compression_level", "3" },
348 { NULL },
349 };
350
351 const FFCodec ff_libspeex_encoder = {
352 .p.name = "libspeex",
353 .p.long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
354 .p.type = AVMEDIA_TYPE_AUDIO,
355 .p.id = AV_CODEC_ID_SPEEX,
356 .priv_data_size = sizeof(LibSpeexEncContext),
357 .init = encode_init,
358 FF_CODEC_ENCODE_CB(encode_frame),
359 .close = encode_close,
360 .p.capabilities = AV_CODEC_CAP_DELAY,
361 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
362 AV_SAMPLE_FMT_NONE },
363 #if FF_API_OLD_CHANNEL_LAYOUT
364 .p.channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
365 AV_CH_LAYOUT_STEREO,
366 0 },
367 #endif
368 .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO,
369 AV_CHANNEL_LAYOUT_STEREO,
370 { 0 },
371 },
372 .p.supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
373 .p.priv_class = &speex_class,
374 .defaults = defaults,
375 .p.wrapper_name = "libspeex",
376 };
377