84 #include <speex/speex.h>
85 #include <speex/speex_header.h>
86 #include <speex/speex_stereo.h>
114 const char *mode_str =
"unknown";
117 switch (
s->header.mode) {
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;
138 s->frames_per_packet);
148 const SpeexMode *
mode;
156 "mono are supported\n", avctx->
channels);
162 case 8000:
mode = speex_lib_get_mode(SPEEX_MODEID_NB);
break;
163 case 16000:
mode = speex_lib_get_mode(SPEEX_MODEID_WB);
break;
164 case 32000:
mode = speex_lib_get_mode(SPEEX_MODEID_UWB);
break;
167 "Resample to 8, 16, or 32 kHz.\n", avctx->
sample_rate);
172 s->enc_state = speex_encoder_init(
mode);
184 speex_encoder_ctl(
s->enc_state, SPEEX_SET_VBR, &
s->header.vbr);
187 speex_encoder_ctl(
s->enc_state, SPEEX_SET_VBR_QUALITY, &
s->vbr_quality);
193 speex_encoder_ctl(
s->enc_state, SPEEX_SET_ABR,
195 speex_encoder_ctl(
s->enc_state, SPEEX_GET_ABR,
198 speex_encoder_ctl(
s->enc_state, SPEEX_SET_BITRATE,
200 speex_encoder_ctl(
s->enc_state, SPEEX_GET_BITRATE,
205 speex_encoder_ctl(
s->enc_state, SPEEX_SET_QUALITY,
207 speex_encoder_ctl(
s->enc_state, SPEEX_GET_BITRATE,
217 speex_encoder_ctl(
s->enc_state, SPEEX_SET_VAD, &
s->vad);
221 speex_encoder_ctl(
s->enc_state, SPEEX_SET_DTX, &
s->dtx);
222 if (!(
s->abr ||
s->vad ||
s->header.vbr))
229 speex_encoder_ctl(
s->enc_state, SPEEX_SET_COMPLEXITY, &complexity);
231 speex_encoder_ctl(
s->enc_state, SPEEX_GET_COMPLEXITY, &complexity);
236 s->header.frames_per_packet =
s->frames_per_packet;
239 speex_encoder_ctl(
s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->
initial_padding);
245 header_data = speex_header_to_packet(&
s->header, &header_size);
250 speex_header_free(header_data);
251 speex_encoder_destroy(
s->enc_state);
257 memcpy(avctx->
extradata, header_data, header_size);
259 speex_header_free(header_data);
262 speex_bits_init(&
s->bits);
278 speex_encode_stereo_int(samples,
s->header.frame_size, &
s->bits);
279 speex_encode_int(
s->enc_state, samples, &
s->bits);
280 s->pkt_frame_count++;
285 if (!
s->pkt_frame_count)
288 while (
s->pkt_frame_count <
s->frames_per_packet) {
289 speex_bits_pack(&
s->bits, 15, 5);
290 s->pkt_frame_count++;
295 if (
s->pkt_frame_count ==
s->frames_per_packet) {
296 s->pkt_frame_count = 0;
299 ret = speex_bits_write(&
s->bits, avpkt->
data, avpkt->
size);
300 speex_bits_reset(&
s->bits);
317 speex_bits_destroy(&
s->bits);
318 speex_encoder_destroy(
s->enc_state);
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 },
346 {
"compression_level",
"3" },
365 .supported_samplerates = (
const int[]){ 8000, 16000, 32000, 0 },
368 .wrapper_name =
"libspeex",
static enum AVSampleFormat sample_fmts[]
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Libavcodec external API header.
#define FF_COMPRESSION_DEFAULT
static av_cold int init(AVCodecContext *avctx)
audio channel layout utility functions
common internal and external API header
mode
Use these values in ebur128_init (or'ed).
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define LIBAVUTIL_VERSION_INT
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static const AVCodecDefault defaults[]
static av_cold void print_enc_params(AVCodecContext *avctx, LibSpeexEncContext *s)
static const AVOption options[]
static const AVClass speex_class
static av_cold int encode_init(AVCodecContext *avctx)
static av_cold int encode_close(AVCodecContext *avctx)
AVCodec ff_libspeex_encoder
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
SpeexBits bits
libspeex bitwriter context
AudioFrameQueue afq
frame queue
int vad
flag to enable VAD
SpeexHeader header
libspeex header struct
int pkt_frame_count
frame count for the current packet
int frames_per_packet
number of frames to encode in each packet
float vbr_quality
VBR quality 0.0 to 10.0.
int cbr_quality
CBR quality 0 to 10.
void * enc_state
libspeex encoder state
int abr
flag to enable ABR
int dtx
flag to enable DTX