yading@10: /* yading@10: * Copyright (C) 2009 Justin Ruggles yading@10: * Copyright (c) 2009 Xuggle Incorporated yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * libspeex Speex audio encoder yading@10: * yading@10: * Usage Guide yading@10: * This explains the values that need to be set prior to initialization in yading@10: * order to control various encoding parameters. yading@10: * yading@10: * Channels yading@10: * Speex only supports mono or stereo, so avctx->channels must be set to yading@10: * 1 or 2. yading@10: * yading@10: * Sample Rate / Encoding Mode yading@10: * Speex has 3 modes, each of which uses a specific sample rate. yading@10: * narrowband : 8 kHz yading@10: * wideband : 16 kHz yading@10: * ultra-wideband : 32 kHz yading@10: * avctx->sample_rate must be set to one of these 3 values. This will be yading@10: * used to set the encoding mode. yading@10: * yading@10: * Rate Control yading@10: * VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags. yading@10: * avctx->global_quality is used to set the encoding quality. yading@10: * For CBR mode, avctx->bit_rate can be used to set the constant bitrate. yading@10: * Alternatively, the 'cbr_quality' option can be set from 0 to 10 to set yading@10: * a constant bitrate based on quality. yading@10: * For ABR mode, set avctx->bit_rate and set the 'abr' option to 1. yading@10: * Approx. Bitrate Range: yading@10: * narrowband : 2400 - 25600 bps yading@10: * wideband : 4000 - 43200 bps yading@10: * ultra-wideband : 4400 - 45200 bps yading@10: * yading@10: * Complexity yading@10: * Encoding complexity is controlled by setting avctx->compression_level. yading@10: * The valid range is 0 to 10. A higher setting gives generally better yading@10: * quality at the expense of encoding speed. This does not affect the yading@10: * bit rate. yading@10: * yading@10: * Frames-per-Packet yading@10: * The encoder defaults to using 1 frame-per-packet. However, it is yading@10: * sometimes desirable to use multiple frames-per-packet to reduce the yading@10: * amount of container overhead. This can be done by setting the yading@10: * 'frames_per_packet' option to a value 1 to 8. yading@10: * yading@10: * yading@10: * Optional features yading@10: * Speex encoder supports several optional features, which can be useful yading@10: * for some conditions. yading@10: * yading@10: * Voice Activity Detection yading@10: * When enabled, voice activity detection detects whether the audio yading@10: * being encoded is speech or silence/background noise. VAD is always yading@10: * implicitly activated when encoding in VBR, so the option is only useful yading@10: * in non-VBR operation. In this case, Speex detects non-speech periods and yading@10: * encodes them with just enough bits to reproduce the background noise. yading@10: * yading@10: * Discontinuous Transmission (DTX) yading@10: * DTX is an addition to VAD/VBR operation, that allows to stop transmitting yading@10: * completely when the background noise is stationary. yading@10: * In file-based operation only 5 bits are used for such frames. yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/common.h" yading@10: #include "libavutil/opt.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: #include "audio_frame_queue.h" yading@10: yading@10: /* TODO: Think about converting abr, vad, dtx and such flags to a bit field */ yading@10: typedef struct { yading@10: AVClass *class; ///< AVClass for private options yading@10: SpeexBits bits; ///< libspeex bitwriter context yading@10: SpeexHeader header; ///< libspeex header struct yading@10: void *enc_state; ///< libspeex encoder state yading@10: int frames_per_packet; ///< number of frames to encode in each packet yading@10: float vbr_quality; ///< VBR quality 0.0 to 10.0 yading@10: int cbr_quality; ///< CBR quality 0 to 10 yading@10: int abr; ///< flag to enable ABR yading@10: int vad; ///< flag to enable VAD yading@10: int dtx; ///< flag to enable DTX yading@10: int pkt_frame_count; ///< frame count for the current packet yading@10: AudioFrameQueue afq; ///< frame queue yading@10: } LibSpeexEncContext; yading@10: yading@10: static av_cold void print_enc_params(AVCodecContext *avctx, yading@10: LibSpeexEncContext *s) yading@10: { yading@10: const char *mode_str = "unknown"; yading@10: yading@10: av_log(avctx, AV_LOG_DEBUG, "channels: %d\n", avctx->channels); yading@10: switch (s->header.mode) { yading@10: case SPEEX_MODEID_NB: mode_str = "narrowband"; break; yading@10: case SPEEX_MODEID_WB: mode_str = "wideband"; break; yading@10: case SPEEX_MODEID_UWB: mode_str = "ultra-wideband"; break; yading@10: } yading@10: av_log(avctx, AV_LOG_DEBUG, "mode: %s\n", mode_str); yading@10: if (s->header.vbr) { yading@10: av_log(avctx, AV_LOG_DEBUG, "rate control: VBR\n"); yading@10: av_log(avctx, AV_LOG_DEBUG, " quality: %f\n", s->vbr_quality); yading@10: } else if (s->abr) { yading@10: av_log(avctx, AV_LOG_DEBUG, "rate control: ABR\n"); yading@10: av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate); yading@10: } else { yading@10: av_log(avctx, AV_LOG_DEBUG, "rate control: CBR\n"); yading@10: av_log(avctx, AV_LOG_DEBUG, " bitrate: %d bps\n", avctx->bit_rate); yading@10: } yading@10: av_log(avctx, AV_LOG_DEBUG, "complexity: %d\n", yading@10: avctx->compression_level); yading@10: av_log(avctx, AV_LOG_DEBUG, "frame size: %d samples\n", yading@10: avctx->frame_size); yading@10: av_log(avctx, AV_LOG_DEBUG, "frames per packet: %d\n", yading@10: s->frames_per_packet); yading@10: av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n", yading@10: avctx->frame_size * s->frames_per_packet); yading@10: av_log(avctx, AV_LOG_DEBUG, "voice activity detection: %d\n", s->vad); yading@10: av_log(avctx, AV_LOG_DEBUG, "discontinuous transmission: %d\n", s->dtx); yading@10: } yading@10: yading@10: static av_cold int encode_init(AVCodecContext *avctx) yading@10: { yading@10: LibSpeexEncContext *s = avctx->priv_data; yading@10: const SpeexMode *mode; yading@10: uint8_t *header_data; yading@10: int header_size; yading@10: int32_t complexity; yading@10: yading@10: /* channels */ yading@10: if (avctx->channels < 1 || avctx->channels > 2) { yading@10: av_log(avctx, AV_LOG_ERROR, "Invalid channels (%d). Only stereo and " yading@10: "mono are supported\n", avctx->channels); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: /* sample rate and encoding mode */ yading@10: switch (avctx->sample_rate) { yading@10: case 8000: mode = &speex_nb_mode; break; yading@10: case 16000: mode = &speex_wb_mode; break; yading@10: case 32000: mode = &speex_uwb_mode; break; yading@10: default: yading@10: av_log(avctx, AV_LOG_ERROR, "Sample rate of %d Hz is not supported. " yading@10: "Resample to 8, 16, or 32 kHz.\n", avctx->sample_rate); yading@10: return AVERROR(EINVAL); yading@10: } yading@10: yading@10: /* initialize libspeex */ yading@10: s->enc_state = speex_encoder_init(mode); yading@10: if (!s->enc_state) { yading@10: av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex\n"); yading@10: return -1; yading@10: } yading@10: speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode); yading@10: yading@10: /* rate control method and parameters */ yading@10: if (avctx->flags & CODEC_FLAG_QSCALE) { yading@10: /* VBR */ yading@10: s->header.vbr = 1; yading@10: s->vad = 1; /* VAD is always implicitly activated for VBR */ yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr); yading@10: s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA, yading@10: 0.0f, 10.0f); yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR_QUALITY, &s->vbr_quality); yading@10: } else { yading@10: s->header.bitrate = avctx->bit_rate; yading@10: if (avctx->bit_rate > 0) { yading@10: /* CBR or ABR by bitrate */ yading@10: if (s->abr) { yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_ABR, yading@10: &s->header.bitrate); yading@10: speex_encoder_ctl(s->enc_state, SPEEX_GET_ABR, yading@10: &s->header.bitrate); yading@10: } else { yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_BITRATE, yading@10: &s->header.bitrate); yading@10: speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, yading@10: &s->header.bitrate); yading@10: } yading@10: } else { yading@10: /* CBR by quality */ yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_QUALITY, yading@10: &s->cbr_quality); yading@10: speex_encoder_ctl(s->enc_state, SPEEX_GET_BITRATE, yading@10: &s->header.bitrate); yading@10: } yading@10: /* stereo side information adds about 800 bps to the base bitrate */ yading@10: /* TODO: this should be calculated exactly */ yading@10: avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0); yading@10: } yading@10: yading@10: /* VAD is activated with VBR or can be turned on by itself */ yading@10: if (s->vad) yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_VAD, &s->vad); yading@10: yading@10: /* Activiting Discontinuous Transmission */ yading@10: if (s->dtx) { yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_DTX, &s->dtx); yading@10: if (!(s->abr || s->vad || s->header.vbr)) yading@10: av_log(avctx, AV_LOG_WARNING, "DTX is not much of use without ABR, VAD or VBR\n"); yading@10: } yading@10: yading@10: /* set encoding complexity */ yading@10: if (avctx->compression_level > FF_COMPRESSION_DEFAULT) { yading@10: complexity = av_clip(avctx->compression_level, 0, 10); yading@10: speex_encoder_ctl(s->enc_state, SPEEX_SET_COMPLEXITY, &complexity); yading@10: } yading@10: speex_encoder_ctl(s->enc_state, SPEEX_GET_COMPLEXITY, &complexity); yading@10: avctx->compression_level = complexity; yading@10: yading@10: /* set packet size */ yading@10: avctx->frame_size = s->header.frame_size; yading@10: s->header.frames_per_packet = s->frames_per_packet; yading@10: yading@10: /* set encoding delay */ yading@10: speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->delay); yading@10: ff_af_queue_init(avctx, &s->afq); yading@10: yading@10: /* create header packet bytes from header struct */ yading@10: /* note: libspeex allocates the memory for header_data, which is freed yading@10: below with speex_header_free() */ yading@10: header_data = speex_header_to_packet(&s->header, &header_size); yading@10: yading@10: /* allocate extradata and coded_frame */ yading@10: avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE); yading@10: if (!avctx->extradata) { yading@10: speex_header_free(header_data); yading@10: speex_encoder_destroy(s->enc_state); yading@10: av_log(avctx, AV_LOG_ERROR, "memory allocation error\n"); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: yading@10: /* copy header packet to extradata */ yading@10: memcpy(avctx->extradata, header_data, header_size); yading@10: avctx->extradata_size = header_size; yading@10: speex_header_free(header_data); yading@10: yading@10: /* init libspeex bitwriter */ yading@10: speex_bits_init(&s->bits); yading@10: yading@10: print_enc_params(avctx, s); yading@10: return 0; yading@10: } yading@10: yading@10: static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt, yading@10: const AVFrame *frame, int *got_packet_ptr) yading@10: { yading@10: LibSpeexEncContext *s = avctx->priv_data; yading@10: int16_t *samples = frame ? (int16_t *)frame->data[0] : NULL; yading@10: int ret; yading@10: yading@10: if (samples) { yading@10: /* encode Speex frame */ yading@10: if (avctx->channels == 2) yading@10: speex_encode_stereo_int(samples, s->header.frame_size, &s->bits); yading@10: speex_encode_int(s->enc_state, samples, &s->bits); yading@10: s->pkt_frame_count++; yading@10: if ((ret = ff_af_queue_add(&s->afq, frame)) < 0) yading@10: return ret; yading@10: } else { yading@10: /* handle end-of-stream */ yading@10: if (!s->pkt_frame_count) yading@10: return 0; yading@10: /* add extra terminator codes for unused frames in last packet */ yading@10: while (s->pkt_frame_count < s->frames_per_packet) { yading@10: speex_bits_pack(&s->bits, 15, 5); yading@10: s->pkt_frame_count++; yading@10: } yading@10: } yading@10: yading@10: /* write output if all frames for the packet have been encoded */ yading@10: if (s->pkt_frame_count == s->frames_per_packet) { yading@10: s->pkt_frame_count = 0; yading@10: if ((ret = ff_alloc_packet2(avctx, avpkt, speex_bits_nbytes(&s->bits))) < 0) yading@10: return ret; yading@10: ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size); yading@10: speex_bits_reset(&s->bits); yading@10: yading@10: /* Get the next frame pts/duration */ yading@10: ff_af_queue_remove(&s->afq, s->frames_per_packet * avctx->frame_size, yading@10: &avpkt->pts, &avpkt->duration); yading@10: yading@10: avpkt->size = ret; yading@10: *got_packet_ptr = 1; yading@10: return 0; yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold int encode_close(AVCodecContext *avctx) yading@10: { yading@10: LibSpeexEncContext *s = avctx->priv_data; yading@10: yading@10: speex_bits_destroy(&s->bits); yading@10: speex_encoder_destroy(s->enc_state); yading@10: yading@10: ff_af_queue_close(&s->afq); yading@10: av_freep(&avctx->extradata); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: #define OFFSET(x) offsetof(LibSpeexEncContext, x) yading@10: #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM yading@10: static const AVOption options[] = { yading@10: { "abr", "Use average bit rate", OFFSET(abr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE }, yading@10: { "cbr_quality", "Set quality value (0 to 10) for CBR", OFFSET(cbr_quality), AV_OPT_TYPE_INT, { .i64 = 8 }, 0, 10, AE }, yading@10: { "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: { "vad", "Voice Activity Detection", OFFSET(vad), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE }, yading@10: { "dtx", "Discontinuous Transmission", OFFSET(dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE }, yading@10: { NULL }, yading@10: }; yading@10: yading@10: static const AVClass class = { yading@10: .class_name = "libspeex", yading@10: .item_name = av_default_item_name, yading@10: .option = options, yading@10: .version = LIBAVUTIL_VERSION_INT, yading@10: }; yading@10: yading@10: static const AVCodecDefault defaults[] = { yading@10: { "b", "0" }, yading@10: { "compression_level", "3" }, yading@10: { NULL }, yading@10: }; yading@10: yading@10: AVCodec ff_libspeex_encoder = { yading@10: .name = "libspeex", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_SPEEX, yading@10: .priv_data_size = sizeof(LibSpeexEncContext), yading@10: .init = encode_init, yading@10: .encode2 = encode_frame, yading@10: .close = encode_close, yading@10: .capabilities = CODEC_CAP_DELAY, yading@10: .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO, yading@10: AV_CH_LAYOUT_STEREO, yading@10: 0 }, yading@10: .supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 }, yading@10: .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"), yading@10: .priv_class = &class, yading@10: .defaults = defaults, yading@10: };