]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libspeexenc.c
svq3: rip out the svq3-relevant parts of pred_motion() out of h264
[ffmpeg] / libavcodec / libspeexenc.c
index 99fe2fe8e1e11b9feb4dac566959cd4c0854e74a..88e5d2fea43070fbb2f6b0eebeda1cfa3e4cf3f2 100644 (file)
@@ -40,7 +40,7 @@
  *     used to set the encoding mode.
  *
  * Rate Control
- *     VBR mode is turned on by setting CODEC_FLAG_QSCALE in avctx->flags.
+ *     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
  *     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 allows to stop transmitting
+ *     completely when the background noise is stationary.
+ *     In file-based operation only 5 bits are used for such frames.
  */
 
 #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 "internal.h"
+#include "audio_frame_queue.h"
 
-typedef struct {
+typedef struct LibSpeexEncContext {
     AVClass *class;             ///< AVClass for private options
     SpeexBits bits;             ///< libspeex bitwriter context
     SpeexHeader header;         ///< libspeex header struct
@@ -80,9 +101,10 @@ typedef struct {
     float vbr_quality;          ///< VBR quality 0.0 to 10.0
     int cbr_quality;            ///< CBR quality 0 to 10
     int abr;                    ///< flag to enable ABR
+    int vad;                    ///< flag to enable VAD
+    int dtx;                    ///< flag to enable DTX
     int pkt_frame_count;        ///< frame count for the current packet
-    int64_t next_pts;           ///< next pts, in sample_rate time base
-    int pkt_sample_count;       ///< sample count in the current packet
+    AudioFrameQueue afq;        ///< frame queue
 } LibSpeexEncContext;
 
 static av_cold void print_enc_params(AVCodecContext *avctx,
@@ -115,6 +137,8 @@ static av_cold void print_enc_params(AVCodecContext *avctx,
            s->frames_per_packet);
     av_log(avctx, AV_LOG_DEBUG, "packet size: %d\n",
            avctx->frame_size * s->frames_per_packet);
+    av_log(avctx, AV_LOG_DEBUG, "voice activity detection: %d\n", s->vad);
+    av_log(avctx, AV_LOG_DEBUG, "discontinuous transmission: %d\n", s->dtx);
 }
 
 static av_cold int encode_init(AVCodecContext *avctx)
@@ -152,9 +176,10 @@ static av_cold int encode_init(AVCodecContext *avctx)
     speex_init_header(&s->header, avctx->sample_rate, avctx->channels, mode);
 
     /* rate control method and parameters */
-    if (avctx->flags & CODEC_FLAG_QSCALE) {
+    if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
         /* VBR */
         s->header.vbr = 1;
+        s->vad = 1; /* VAD is always implicitly activated for VBR */
         speex_encoder_ctl(s->enc_state, SPEEX_SET_VBR, &s->header.vbr);
         s->vbr_quality = av_clipf(avctx->global_quality / (float)FF_QP2LAMBDA,
                                   0.0f, 10.0f);
@@ -186,6 +211,17 @@ static av_cold int encode_init(AVCodecContext *avctx)
         avctx->bit_rate = s->header.bitrate + (avctx->channels == 2 ? 800 : 0);
     }
 
+    /* VAD is activated with VBR or can be turned on by itself */
+    if (s->vad)
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_VAD, &s->vad);
+
+    /* Activiting Discontinuous Transmission */
+    if (s->dtx) {
+        speex_encoder_ctl(s->enc_state, SPEEX_SET_DTX, &s->dtx);
+        if (!(s->abr || s->vad || s->header.vbr))
+            av_log(avctx, AV_LOG_WARNING, "DTX is not much of use without ABR, VAD or VBR\n");
+    }
+
     /* set encoding complexity */
     if (avctx->compression_level > FF_COMPRESSION_DEFAULT) {
         complexity = av_clip(avctx->compression_level, 0, 10);
@@ -199,17 +235,17 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->header.frames_per_packet = s->frames_per_packet;
 
     /* set encoding delay */
-    speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->delay);
+    speex_encoder_ctl(s->enc_state, SPEEX_GET_LOOKAHEAD, &avctx->initial_padding);
+    ff_af_queue_init(avctx, &s->afq);
 
     /* create header packet bytes from header struct */
     /* note: libspeex allocates the memory for header_data, which is freed
              below with speex_header_free() */
     header_data = speex_header_to_packet(&s->header, &header_size);
 
-    /* allocate extradata and coded_frame */
-    avctx->extradata   = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->extradata || !avctx->coded_frame) {
+    /* allocate extradata */
+    avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!avctx->extradata) {
         speex_header_free(header_data);
         speex_encoder_destroy(s->enc_state);
         av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
@@ -228,19 +264,21 @@ static av_cold int encode_init(AVCodecContext *avctx)
     return 0;
 }
 
-static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
-                        void *data)
+static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
+                        const AVFrame *frame, int *got_packet_ptr)
 {
     LibSpeexEncContext *s = avctx->priv_data;
-    int16_t *samples      = data;
+    int16_t *samples      = frame ? (int16_t *)frame->data[0] : NULL;
+    int ret;
 
-    if (data) {
+    if (samples) {
         /* encode Speex frame */
         if (avctx->channels == 2)
             speex_encode_stereo_int(samples, s->header.frame_size, &s->bits);
         speex_encode_int(s->enc_state, samples, &s->bits);
         s->pkt_frame_count++;
-        s->pkt_sample_count += avctx->frame_size;
+        if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
+            return ret;
     } else {
         /* handle end-of-stream */
         if (!s->pkt_frame_count)
@@ -255,18 +293,20 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
     /* write output if all frames for the packet have been encoded */
     if (s->pkt_frame_count == s->frames_per_packet) {
         s->pkt_frame_count = 0;
-        avctx->coded_frame->pts = ff_samples_to_time_base(avctx, s->next_pts -
-                                                          avctx->delay);
-        s->next_pts += s->pkt_sample_count;
-        s->pkt_sample_count = 0;
-        if (buf_size > speex_bits_nbytes(&s->bits)) {
-            int ret = speex_bits_write(&s->bits, frame, buf_size);
-            speex_bits_reset(&s->bits);
+        if ((ret = ff_alloc_packet(avpkt, speex_bits_nbytes(&s->bits)))) {
+            av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
             return ret;
-        } else {
-            av_log(avctx, AV_LOG_ERROR, "output buffer too small");
-            return AVERROR(EINVAL);
         }
+        ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size);
+        speex_bits_reset(&s->bits);
+
+        /* Get the next frame pts/duration */
+        ff_af_queue_remove(&s->afq, s->frames_per_packet * avctx->frame_size,
+                           &avpkt->pts, &avpkt->duration);
+
+        avpkt->size = ret;
+        *got_packet_ptr = 1;
+        return 0;
     }
     return 0;
 }
@@ -278,7 +318,7 @@ static av_cold int encode_close(AVCodecContext *avctx)
     speex_bits_destroy(&s->bits);
     speex_encoder_destroy(s->enc_state);
 
-    av_freep(&avctx->coded_frame);
+    ff_af_queue_close(&s->afq);
     av_freep(&avctx->extradata);
 
     return 0;
@@ -287,9 +327,11 @@ static av_cold int encode_close(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(LibSpeexEncContext, x)
 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
-    { "abr",               "Use average bit rate",                      OFFSET(abr),               AV_OPT_TYPE_INT, { 0 }, 0,   1, AE },
-    { "cbr_quality",       "Set quality value (0 to 10) for CBR",       OFFSET(cbr_quality),       AV_OPT_TYPE_INT, { 8 }, 0,  10, AE },
-    { "frames_per_packet", "Number of frames to encode in each packet", OFFSET(frames_per_packet), AV_OPT_TYPE_INT, { 1 }, 1,   8, AE },
+    { "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 },
 };
 
@@ -308,15 +350,20 @@ static const AVCodecDefault defaults[] = {
 
 AVCodec ff_libspeex_encoder = {
     .name           = "libspeex",
+    .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
     .type           = AVMEDIA_TYPE_AUDIO,
-    .id             = CODEC_ID_SPEEX,
+    .id             = AV_CODEC_ID_SPEEX,
     .priv_data_size = sizeof(LibSpeexEncContext),
     .init           = encode_init,
-    .encode         = encode_frame,
+    .encode2        = encode_frame,
     .close          = encode_close,
-    .capabilities   = CODEC_CAP_DELAY,
-    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
-    .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
+    .capabilities   = AV_CODEC_CAP_DELAY,
+    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_NONE },
+    .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
+                                           AV_CH_LAYOUT_STEREO,
+                                           0 },
+    .supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
     .priv_class     = &class,
     .defaults       = defaults,
 };