FFmpeg
Data Structures | Macros | Functions | Variables
libspeexenc.c File Reference
#include <speex/speex.h>
#include <speex/speex_header.h>
#include <speex/speex_stereo.h>
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "encode.h"
#include "internal.h"
#include "audio_frame_queue.h"

Go to the source code of this file.

Data Structures

struct  LibSpeexEncContext
 

Macros

#define OFFSET(x)   offsetof(LibSpeexEncContext, x)
 
#define AE   AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 

Functions

static av_cold void print_enc_params (AVCodecContext *avctx, LibSpeexEncContext *s)
 
static av_cold int encode_init (AVCodecContext *avctx)
 
static int encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
 
static av_cold int encode_close (AVCodecContext *avctx)
 

Variables

static const AVOption options []
 
static const AVClass speex_class
 
static const AVCodecDefault defaults []
 
const AVCodec ff_libspeex_encoder
 

Detailed Description

libspeex Speex audio encoder

Usage Guide This explains the values that need to be set prior to initialization in order to control various encoding parameters.

Channels Speex only supports mono or stereo, so avctx->channels must be set to 1 or 2.

Sample Rate / Encoding Mode Speex has 3 modes, each of which uses a specific sample rate. narrowband : 8 kHz wideband : 16 kHz ultra-wideband : 32 kHz avctx->sample_rate must be set to one of these 3 values. This will be used to set the encoding mode.

Rate Control VBR mode is turned on by setting AV_CODEC_FLAG_QSCALE in avctx->flags. avctx->global_quality is used to set the encoding quality. For CBR mode, avctx->bit_rate can be used to set the constant bitrate. Alternatively, the 'cbr_quality' option can be set from 0 to 10 to set a constant bitrate based on quality. For ABR mode, set avctx->bit_rate and set the 'abr' option to 1. Approx. Bitrate Range: narrowband : 2400 - 25600 bps wideband : 4000 - 43200 bps ultra-wideband : 4400 - 45200 bps

Complexity Encoding complexity is controlled by setting avctx->compression_level. The valid range is 0 to 10. A higher setting gives generally better quality at the expense of encoding speed. This does not affect the bit rate.

Frames-per-Packet The encoder defaults to using 1 frame-per-packet. However, it is sometimes desirable to use multiple frames-per-packet to reduce the amount of container overhead. This can be done by setting the 'frames_per_packet' option to a value 1 to 8.

Optional features Speex encoder supports several optional features, which can be useful for some conditions.

Voice Activity Detection When enabled, voice activity detection detects whether the audio being encoded is speech or silence/background noise. VAD is always implicitly activated when encoding in VBR, so the option is only useful in non-VBR operation. In this case, Speex detects non-speech periods and encodes them with just enough bits to reproduce the background noise.

Discontinuous Transmission (DTX) DTX is an addition to VAD/VBR operation, that makes it possible to stop transmitting completely when the background noise is stationary. In file-based operation only 5 bits are used for such frames.

Definition in file libspeexenc.c.

Macro Definition Documentation

◆ OFFSET

#define OFFSET (   x)    offsetof(LibSpeexEncContext, x)

Definition at line 326 of file libspeexenc.c.

◆ AE

Definition at line 327 of file libspeexenc.c.

Function Documentation

◆ print_enc_params()

static av_cold void print_enc_params ( AVCodecContext avctx,
LibSpeexEncContext s 
)
static

Definition at line 112 of file libspeexenc.c.

Referenced by encode_init().

◆ encode_init()

static av_cold int encode_init ( AVCodecContext avctx)
static

Definition at line 146 of file libspeexenc.c.

◆ encode_frame()

static int encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int got_packet_ptr 
)
static

Definition at line 269 of file libspeexenc.c.

◆ encode_close()

static av_cold int encode_close ( AVCodecContext avctx)
static

Definition at line 314 of file libspeexenc.c.

Variable Documentation

◆ options

const AVOption options[]
static
Initial value:
= {
{ "abr", "Use average bit rate", OFFSET(abr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
{ "cbr_quality", "Set quality value (0 to 10) for CBR", OFFSET(cbr_quality), AV_OPT_TYPE_INT, { .i64 = 8 }, 0, 10, AE },
{ "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 8, AE },
{ "vad", "Voice Activity Detection", OFFSET(vad), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
{ "dtx", "Discontinuous Transmission", OFFSET(dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE },
{ NULL },
}

Definition at line 328 of file libspeexenc.c.

◆ speex_class

const AVClass speex_class
static
Initial value:
= {
.class_name = "libspeex",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 337 of file libspeexenc.c.

◆ defaults

const AVCodecDefault defaults[]
static
Initial value:
= {
{ "b", "0" },
{ "compression_level", "3" },
{ NULL },
}

Definition at line 344 of file libspeexenc.c.

◆ ff_libspeex_encoder

const AVCodec ff_libspeex_encoder
Initial value:
= {
.name = "libspeex",
.long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
.priv_data_size = sizeof(LibSpeexEncContext),
.encode2 = encode_frame,
.close = encode_close,
.capabilities = AV_CODEC_CAP_DELAY,
.channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
0 },
.supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
.priv_class = &speex_class,
.defaults = defaults,
.wrapper_name = "libspeex",
}

Definition at line 350 of file libspeexenc.c.

init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
encode_close
static av_cold int encode_close(AVCodecContext *avctx)
Definition: libspeexenc.c:314
AV_CODEC_ID_SPEEX
@ AV_CODEC_ID_SPEEX
Definition: codec_id.h:457
AE
#define AE
Definition: libspeexenc.c:327
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
options
static const AVOption options[]
Definition: libspeexenc.c:328
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
speex_class
static const AVClass speex_class
Definition: libspeexenc.c:337
defaults
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:344
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libspeexenc.c:269
OFFSET
#define OFFSET(x)
Definition: libspeexenc.c:326
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
LibSpeexEncContext
Definition: libspeexenc.c:97
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: libspeexenc.c:146